Find the Lexicographically Largest String From the Box II - Problem

Alice is hosting an exciting word game for her friends! Here's how it works:

Given a string word and an integer numFriends, Alice runs multiple rounds where each round splits the word into exactly numFriends non-empty substrings. The key rule: no two rounds can have the identical split pattern.

After each round, all the split substrings go into a magical box. Your mission is to find the lexicographically largest string that ends up in the box after all possible rounds are completed.

Lexicographic Order: String a is lexicographically smaller than string b if at the first differing position, a has a character that appears earlier in the alphabet than b. If one string is a prefix of another, the shorter one is lexicographically smaller.

Example: With word="abc" and numFriends=2, possible splits are: ["a","bc"], ["ab","c"]. The box contains {"a", "bc", "ab", "c"}, so the lexicographically largest is "c".

Input & Output

example_1.py โ€” Basic Split
$ Input: word = "abc", numFriends = 2
โ€บ Output: "c"
๐Ÿ’ก Note: Possible splits: ["a","bc"], ["ab","c"]. All substrings: {"a", "bc", "ab", "c"}. Lexicographically: "a" < "ab" < "bc" < "c". Maximum is "c".
example_2.py โ€” Three Friends
$ Input: word = "abcd", numFriends = 3
โ€บ Output: "d"
๐Ÿ’ก Note: Possible splits: ["a","b","cd"], ["a","bc","d"], ["ab","c","d"]. All substrings: {"a", "b", "cd", "bc", "d", "ab", "c"}. Maximum is "d".
example_3.py โ€” Single Character Result
$ Input: word = "zyxw", numFriends = 2
โ€บ Output: "zyxw"
๐Ÿ’ก Note: One possible split is ["zyxw"], but we need 2 friends, so splits are ["z","yxw"], ["zy","xw"], ["zyx","w"]. The lexicographically largest substring is "zyxw" from the split ["zyxw"] if numFriends=1, but since numFriends=2, we compare "z","yxw","zy","xw","zyx","w". The largest is "zyxw" - wait, that's not possible with numFriends=2. Let me recalculate: splits are ["z","yxw"], ["zy","xw"], ["zyx","w"]. Largest is "zyx".

Constraints

  • 1 โ‰ค word.length โ‰ค 100
  • 1 โ‰ค numFriends โ‰ค word.length
  • word consists of lowercase English letters only
  • The number of possible splits can be exponential

Visualization

Tap to expand
Alice's String Splitting GameOriginal Ribbon:abcCut Pattern 1: Split after 'a'a+bcCut Pattern 2: Split after 'ab'ab+cCollection BoxabcabcWINNER!Lexicographic order: a < ab < bc < c๐ŸŽฏ Algorithm: Generate all possible split patterns,collect all pieces, and find the lexicographically largest!
Understanding the Visualization
1
Setup
Alice has a ribbon with letters 'abc' and 2 friends
2
First Cut
Cut after 'a': pieces are 'a' and 'bc'
3
Second Cut
Cut after 'ab': pieces are 'ab' and 'c'
4
Collect All
Box contains: 'a', 'bc', 'ab', 'c'
5
Find Winner
Compare lexicographically: 'c' wins!
Key Takeaway
๐ŸŽฏ Key Insight: Rather than storing all substrings from all splits, we can track the maximum substring on-the-fly during generation, significantly reducing memory usage while finding the optimal solution.
Asked in
Google 35 Microsoft 28 Amazon 22 Meta 15
23.2K Views
Medium Frequency
~25 min Avg. Time
892 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