Ctrl K

Alphabetically Last Piece

Hard
You are given a string word of lowercase English letters. Consider all of its non-empty substrings, that is, runs of consecutive letters, and imagine them listed in dictionary (lexicographic) order. Return the substring that ends up last in that list.
Example 1
word="tomato" → 'tomato'

The latest letter in the word is t, and it appears twice. Of the pieces tomato and to, tomato comes later in a dictionary.

Example 2
word="cacao" → 'o'

The answer must start with o, so it is just o.

Example 3
word="zzyzz" → 'zzyzz'
Constraints
  • 1 ≤ len(word) ≤ 4 · 10⁵
  • word consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMirror Chunk Split HardNext Mirror Number HardPalindrome by Adjacent Swaps HardDisjoint Long Palindromes HardCheapest Cut to a Subsequence Hard
loading Python…
Input
word="tomato"
Expected
'tomato'

Run checks the first 3 tests in your browser. Submit checks all 82 tests on the server, hidden ones included.