Talentd
FleetCode

1163. Last Substring in Lexicographical Order

Hard35.1% acceptance5 companies

Given a string s, return the last substring of s in lexicographical order.

Example 1:

Input: s = "abab"
Output: "bab"
Explanation: The substrings are ["a", "ab", "aba", "abab", "b", "ba", "bab"]. The lexicographically maximum substring is "bab".

Example 2:

Input: s = "leetcode"
Output: "tcode"

Constraints:

  • 1 <= s.length <= 4 * 105
  • s contains only lowercase English letters.

Hints open one at a time

Hint 1
Assume that the answer is a sub-string from index i to j. If you add the character at index j+1 you get a better answer.
Hint 2
The answer is always a suffix of the given string.
Hint 3
Since the limits are high, we need an efficient data structure.
Hint 4
Use suffix array.
23:56
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def lastSubstring(self, s: str) -> str:
3
Starter codePython 3Spaces: 4
Examples
Input
"abab"
Input
"leetcode"
Your code is saved on this device.Solution