956. Find the Lexicographically Largest String From the Box II
Alice is organizing a game for her friends. You are given a string word and an integer numFriends. In the game, the string is split into numFriends contiguous, non-empty segments. Each round of the game uses a unique way of splitting the string, and all the resulting segments are placed into a box.
Your task is to determine the lexicographically largest string present in the box after considering every possible unique split.
How Splitting Works:
- The string word is divided into numFriends parts, each part being a contiguous and non-empty substring.
- No split is repeated across different rounds.
- All segments from every possible split are collected into a single box.
Lexicographical Order:
A string a is considered lexicographically smaller than a string b if at the first position where they differ, the letter in a comes earlier in the alphabet than the corresponding letter in b. If one string is a prefix of the other, then the shorter string is considered smaller.
Example 1:
Input: word = "pqrs", numFriends = 2
Output: “s”
Explanation:
The possible splits of "pqrs" into 2 non-empty segments are:
- "p" and "qrs"
- "pq" and "rs"
- "pqr" and "s"
- Collecting all segments gives: {"p", "qrs", "pq", "rs", "pqr", "s"}.
- When compared lexicographically, "s" is the largest.
Example 2:
Input: word = "zxy", numFriends = 2
Output: “zx”
Explanation:
The splits of "zxy" into 2 parts are:
- "z" and "xy"
- "zx" and "y"
- The segments collected are: {"z", "xy", "zx", "y"}.
- Among these, "zx" is lexicographically the largest.
Now Your Turn!
Pick the correct output for the given inputInput: word = "abz", numFriends = 3
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 ≤ word.length ≤ 2 × 105
- word consists only of lowercase English letters.
- 1 ≤ numFriends ≤ word.length