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:
"zy"
💡 Note:
Possible splits: ["z","y","ab"], ["z","ya","b"], ["zy","a","b"]. All substrings: "z", "y", "ab", "ya", "zy", "a", "b". The lexicographically largest is "zy".
Constraints
- 1 ≤ word.length ≤ 50
- 1 ≤ numFriends ≤ word.length
- word consists of lowercase English letters
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code