Last Substring in Lexicographical Order LeetCode solution in C++

1
class Solution {
2
public:
3
string lastSubstring(string s) {
4
int maxIndex = s.length() - 1;
5

6
for (int currIndex = s.length() - 1; currIndex >= 0; currIndex--) {
7
if (s[currIndex] > s[maxIndex])
8
maxIndex = currIndex;
9

10
else if (s[currIndex] == s[maxIndex]) {
11
int i = currIndex + 1;
12
int j = maxIndex + 1;
13

14
while (i < maxIndex && j < s.length() && s[i] == s[j]) {
15
i++;
16
j++;
17
}
18

19
if (i == maxIndex || j == s.length() || s[i] > s[j]) maxIndex = currIndex;
20
}
21
}
22

23
return s.substr(maxIndex);
24
}
25
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0