Skip to content

Commit a83b32c

Browse files
solves second largest digit in a string
1 parent ddf2d37 commit a83b32c

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@
437437
| 1784 | [Check if Binary String Has at Most One Segment of Ones](https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones) | [![Java](assets/java.png)](src/CheckIfBinaryStringHasAtMostOneSegmentOfOnes.java) | |
438438
| 1790 | [Check if One String Swap Can Make Strings Equal](https://leetcode.com/problems/check-if-one-string-swap-can-make-strings-equal) | [![Java](assets/java.png)](src/CheckIfOneStringSwapCanMakeStringsEqual.java) | |
439439
| 1791 | [Find Center of Star Graph](https://leetcode.com/problems/find-center-of-star-graph) | [![Java](assets/java.png)](src/FindCenterOfStarGraph.java) | |
440-
| 1796 | [Second Largest Digit in a String](https://leetcode.com/problems/second-largest-digit-in-a-string) | | |
440+
| 1796 | [Second Largest Digit in a String](https://leetcode.com/problems/second-largest-digit-in-a-string) | [![Java](assets/java.png)](src/SecondLargestDigitInAString.java) | |
441441
| 1800 | [Maximum Ascending Subarray Sum](https://leetcode.com/problems/maximum-ascending-subarray-sum) | | |
442442
| 1805 | [Number of Different Integers in a String](https://leetcode.com/problems/number-of-different-integers-in-a-string) | | |
443443
| 1812 | [Determine Color of a Chessboard Square](https://leetcode.com/problems/determine-color-of-a-chessboard-square) | | |

src/SecondLargestDigitInAString.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class SecondLargestDigitInAString {
2+
public int secondHighest(String s) {
3+
int largestDigit = -1, secondLargestDigit = -1;
4+
for (int index = 0 ; index < s.length() ; index++) {
5+
if (Character.isDigit(s.charAt(index))) {
6+
int digit = s.charAt(index) - '0';
7+
if (digit > largestDigit) {
8+
int temp = largestDigit;
9+
largestDigit = digit;
10+
secondLargestDigit = temp;
11+
} else if (digit < largestDigit && digit > secondLargestDigit) {
12+
secondLargestDigit = digit;
13+
}
14+
}
15+
}
16+
return secondLargestDigit;
17+
}
18+
}

0 commit comments

Comments
 (0)