Skip to content

Commit 83e7c60

Browse files
authored
Create Largest 3-Same-Digit Number in String.java
1 parent ffc081d commit 83e7c60

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public String largestGoodInteger(String num) {
3+
int largestIdx = -1;
4+
for (int i = 0; i < num.length() - 2; i++) {
5+
if (num.charAt(i) == num.charAt(i + 1) && num.charAt(i + 1) == num.charAt(i + 2)) {
6+
if (largestIdx == -1 || num.substring(largestIdx, largestIdx + 3).compareTo(num.substring(i, i + 3)) < 0) {
7+
largestIdx = i;
8+
}
9+
}
10+
}
11+
return largestIdx == -1 ? "" : num.substring(largestIdx, largestIdx + 3);
12+
}
13+
}

0 commit comments

Comments
 (0)