Last Substring in Lexicographical Order - Problem

Given a string s, you need to find and return the lexicographically largest substring among all possible substrings of s.

In lexicographical order, strings are compared character by character from left to right. For example, "zzb" comes after "zza" because at the third position, 'b' > 'a'. The last substring in lexicographical order is the one that would appear at the end if all substrings were sorted alphabetically.

Example: For string "abab", all substrings are: ["a", "ab", "aba", "abab", "b", "ba", "bab", "a", "ab", "b"]. After removing duplicates and sorting: ["a", "ab", "aba", "abab", "b", "ba", "bab"]. The lexicographically largest is "bab".

Key Insight: The optimal substring must start with the largest character in the string, and we need to find the best starting position among all occurrences of this maximum character.

Input & Output

example_1.py — Basic Example
$ Input: s = "abab"
› Output: "bab"
šŸ’” Note: All substrings are: ["a", "ab", "aba", "abab", "b", "ba", "bab"]. Among these, "bab" is lexicographically largest.
example_2.py — Single Character
$ Input: s = "leetcode"
› Output: "etcode"
šŸ’” Note: The maximum character is 't', but we need to compare all suffixes starting with 'e' (which is the largest character). The suffix "etcode" starting at position 1 is lexicographically largest.
example_3.py — All Same Characters
$ Input: s = "aaaa"
› Output: "aaaa"
šŸ’” Note: When all characters are the same, the entire string is the lexicographically largest substring.

Visualization

Tap to expand
Lexicographically Largest Substring TournamentString: "abcab" → All positions competePosition 0:abcab→ "abcab"Position 1:bcab→ "bcab"Position 2:cab→ "cab" āœ“Position 3:ab→ "ab" (eliminated)Position 4:b→ "b" (eliminated)Tournament Result:"cab" wins because 'c' > 'b' > 'a'Lexicographically Largest: "cab"
Understanding the Visualization
1
Identify Candidates
Every position in the string is a potential starting point for the optimal substring
2
Smart Comparison
Use two pointers to compare suffixes starting at different positions
3
Eliminate Inferior Options
When one suffix is clearly better, eliminate the worse starting position
4
Return Winner
The surviving position gives us the lexicographically largest substring
Key Takeaway
šŸŽÆ Key Insight: The optimal substring always starts at the position that produces the lexicographically largest suffix. We can find this efficiently using two pointers without generating all possible substrings.

Time & Space Complexity

Time Complexity
ā±ļø
O(n³)

O(n²) to generate all substrings, O(n) to compare each substring lexicographically

n
2n
⚠ Quadratic Growth
Space Complexity
O(n²)

Space to store all substrings (there are O(n²) substrings, each of average length O(n))

n
2n
⚠ Quadratic Space

Constraints

  • 1 ≤ s.length ≤ 4 Ɨ 105
  • s contains only lowercase English letters
  • Follow-up: Can you solve this in O(n) time complexity?
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
18.5K Views
Medium Frequency
~25 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
šŸ’” Explanation
AI Ready
šŸ’” Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen