Ctrl K

Best Share of a Long Word

Hard
You are given a word word and an integer parts. The word is cut into parts non-empty consecutive pieces without reordering letters. Every possible way of cutting is tried exactly once, and all pieces from all the ways go into one pile. Return the piece from the pile that comes last in alphabetical (dictionary) order. When one string is the beginning of another, the longer one is larger. The word can be very long, so comparing every possible piece directly will not do.
Example 1
word="melon", parts=2 → 'on'

A piece is at most 4 letters long. The latest letter is o, and only "on" starts with it, yet "on" still beats "melo" and "lon" because o comes after m and l.

Example 2
word="zzaz", parts=3 → 'zz'

A piece is at most 2 letters long. The best one is "zz": it is longer than "z" and larger than "za" and "az".

Example 3
word="plum", parts=1 → 'plum'

Nothing is cut, so the only piece is the whole word.

Constraints
  • 1 ≤ len(word) ≤ 10⁶
  • word consists of lowercase English letters
  • 1 ≤ parts ≤ len(word)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMirror Chunk Split HardAlphabetically Last Piece HardNext Mirror Number HardPalindrome by Adjacent Swaps HardDisjoint Long Palindromes Hard
loading Python…
Input
word="melon", parts=2
Expected
'on'

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