Find the Lexicographically Largest String From the Box I - 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:

  • word is split into numFriends non-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.

Input & Output

Example 1 — Basic Case
$ Input: word = "abc", numFriends = 2
Output: "c"
💡 Note: Possible splits: ["a","bc"], ["ab","c"]. All substrings: "a", "bc", "ab", "c". Lexicographically largest is "c".
Example 2 — Single Friend
$ Input: word = "aba", numFriends = 1
Output: "aba"
💡 Note: Only one split possible: ["aba"]. The answer is the word itself.
Example 3 — Multiple Friends
$ Input: word = "abac", numFriends = 3
Output: "c"
💡 Note: Many possible splits into 3 parts. All possible substrings include "a", "b", "c", "ab", "ba", "ac", etc. The largest is "c".

Constraints

  • 1 ≤ word.length ≤ 103
  • 1 ≤ numFriends ≤ word.length
  • word consists of lowercase English letters

Visualization

Tap to expand
Find Lexicographically Largest String From BoxInput: word = "abc", numFriends = 2Original Word: "abc"All Possible Splits:Split 1: ["a", "bc"]Split 2: ["ab", "c"]Box Contents (All Substrings):"a""bc""ab""c"Lexicographic comparison: "a" < "ab" < "bc" > "c"Output: "c"
Understanding the Visualization
1
Input
Word 'abc' with numFriends = 2
2
Process
All possible splits and their substrings
3
Output
Lexicographically largest substring 'c'
Key Takeaway
🎯 Key Insight: The answer is always the lexicographically largest substring of the original word
Asked in
Google 25 Microsoft 20
23.0K Views
Medium Frequency
~25 min Avg. Time
890 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