Find the Lexicographically Largest String From the Box II - Problem
You are given a string word and an integer numFriends. Alice is organizing a game for her numFriends friends.
There are multiple rounds in the game, where in each round:
- The string
wordis split intonumFriendsnon-empty strings, such that no previous round has had the exact same split. - All the split words are put into a box.
Find the lexicographically largest string from the box after all the rounds are finished.
A string a is lexicographically smaller than a string b if in the first position where a and b differ, string a has a letter that appears earlier in the alphabet than the corresponding letter in b. If the first min(a.length, b.length) characters do not differ, then the shorter string is the lexicographically smaller one.
Input & Output
Example 1 — Basic Case
$
Input:
word = "abc", numFriends = 2
›
Output:
"bc"
💡 Note:
Possible splits: ["a","bc"], ["ab","c"]. All substrings: "a", "bc", "ab", "c". The lexicographically largest is "bc".
Example 2 — Single Friend
$
Input:
word = "hello", numFriends = 1
›
Output:
"hello"
💡 Note:
With only 1 friend, the entire word goes to one person, so the answer is "hello".
Example 3 — Multiple Splits
$
Input:
word = "zyab", numFriends = 3
›
Output:
"zyab"
💡 Note:
One possible split is ["z","y","ab"]. The substring "zyab" (when we can take the whole word) is lexicographically largest.
Constraints
- 1 ≤ word.length ≤ 50
- 1 ≤ numFriends ≤ word.length
- word consists of lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input
word = 'abc', numFriends = 2
2
All Possible Splits
Generate all ways to split into 2 non-empty parts
3
Collect & Compare
Find lexicographically largest substring from all splits
Key Takeaway
🎯 Key Insight: The lexicographically largest substring will always be a suffix of the original word
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code