-
Notifications
You must be signed in to change notification settings - Fork 1
/
Day 29.2.txt
56 lines (45 loc) · 1.27 KB
/
Day 29.2.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
1796. Second Largest Digit in a String
Given an alphanumeric string s, return the second largest numerical digit that appears in s, or -1 if it does not exist.
An alphanumeric string is a string consisting of lowercase English letters and digits.
Example 1:
Input: s = "dfa12321afd"
Output: 2
Explanation: The digits that appear in s are [1, 2, 3]. The second largest digit is 2.
Example 2:
Input: s = "abc1111"
Output: -1
Explanation: The digits that appear in s are [1]. There is no second largest digit.
Constraints:
1 <= s.length <= 500
s consists of only lowercase English letters and/or digits.
class Solution {
public int secondHighest(String s) {
int c[]=new int[10];
Arrays.fill(c,0);
int i,l=0,h=0;
for(i=0;i<s.length();i++)
{
if(s.charAt(i)>='0'&&s.charAt(i)<='9')
{
c[s.charAt(i)-'0']++;
}
}
for(i=0;i<10;i++)
{
if(c[i]>0)
break;
}
if(i==10)
return -1;
for(i=9;i>=0;i--)
{
if(c[i]>0)
l++;
if(l==2)
{
return i;
}
}
return -1;
}
}