Find the Lexicographically Largest String From the Box I

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.

Example 1
Inputword = "dbca", numFriends = 2
Output"dbc"
Among all possible splits into 2 non-empty strings, the largest string placed in the box is "dbc".
Example 2
Inputword = "gggg", numFriends = 4
Output"g"
The only possible split into 4 non-empty strings is four copies of "g", so the largest string is "g".

Constraints

  • 1 <= word.length <= 5 * 10^3
  • word consists only of lowercase English letters.
  • 1 <= numFriends <= word.length

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate