题目描述:
Given a string s
, return the last substring of s
in lexicographical order.
Example 1:
Input: "abab"
Output: "bab"
Explanation: The substrings are ["a", "ab", "aba", "abab", "b", "ba", "bab"]. The lexicographically maximum substring is "bab".
Example 2:
Input: "leetcode"
Output: "tcode"
Note:
1 <= s.length <= 4 * 10^5
- s contains only lowercase English letters.
class Solution {
public:
string lastSubstring(string s) {
int start = 0, N = s.size();
for (int i = 1; i < N; ++i)
{
if (s[i] < s[start]) continue;
else if (s[i] == s[start])
{ // 有连续的max_char出现时,只选第一个
if (s[i - 1] == s[start]) continue;
int j = 0;
while (i + j < N && s[start + j] == s[i + j]) j++;
if(i + j < N && s[start + j] < s[i + j]) start = i;
}
else start = i;
}
return s.substr(start);
}
};