Last Substring in Lexicographical Order LeetCode solution in Java

1
class Solution {
2

3
public String lastSubstring(String s) {
4
int maxIndex = s.length() - 1;
5

6
for (int currIndex = s.length() - 1; currIndex >= 0; currIndex--) {
7
if (s.charAt(currIndex) > s.charAt(maxIndex)) maxIndex = currIndex;
8
else if (s.charAt(currIndex) == s.charAt(maxIndex)) {
9
int i = currIndex + 1;
10
int j = maxIndex + 1;
11

12
while (i < maxIndex && j < s.length() && s.charAt(i) == s.charAt(j)) {
13
i++;
14
j++;
15
}
16

17
if (i == maxIndex || j == s.length() || s.charAt(i) > s.charAt(j)) maxIndex = currIndex;
18
}
19
}
20

21
return s.substring(maxIndex);
22
}
23
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0