Description
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:
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.
Β
Example 1:
Input: word = "dbca", numFriends = 2
Output: "dbc"
Explanation:Β
All possible splits are:
"d"and"bca"."db"and"ca"."dbc"and"a".
Example 2:
Input: word = "gggg", numFriends = 4
Output: "g"
Explanation:Β
The only possible split is: "g", "g", "g", and "g".
Β
Constraints:
1 <= word.length <= 5Β * 103wordconsists only of lowercase English letters.1 <= numFriends <= word.length
Solution
Python3
class Solution:
def answerString(self, word: str, numFriends: int) -> str:
if numFriends == 1: return word
N = len(word)
res = None
for i in range(N):
s = word[i: i + N - numFriends + 1]
if res is None or s > res:
res = s
return res