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 numFriendsnon-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: word ="gggg", numFriends =4Output: "g"Explanation:
The only possible split is:"g","g","g", and "g".### Constraint
-`1 <= word.length <= 2 * 105`-`word` consists only of lowercase English letters.-`1 <= numFriends <= word.length`
For each possible split of the string into numFriends non-empty parts, the largest string in the box is always the lexicographically largest substring among all possible splits. For each split, the largest part is always a contiguous substring, so we can try all possible splits and track the largest substring.
classSolution {
public String getMaxString(String word, int numFriends) {
int n = word.length();
String ans = word.substring(0, 1);
List<Integer> split =new ArrayList<>();
dfs(0, 0, split);
return ans;
}
String ans;
voiddfs(int idx, int last, List<Integer> split) {
int n = ans.length();
if (idx == n-1) {
List<Integer> cuts =new ArrayList<>();
cuts.add(0); cuts.addAll(split); cuts.add(n);
for (int i = 0; i < n; i++) {
int l = cuts.get(i), r = cuts.get(i+1);
String s = ans.substring(l, r);
if (s.length() > ans.length() || (s.length() == ans.length() && s.compareTo(ans) > 0)) ans = s;
}
return;
}
for (int i = last+1; i < n-(n-idx-1); i++) {
split.add(i);
dfs(idx+1, i, split);
split.remove(split.size()-1);
}
}
}
classSolution {
fungetMaxString(word: String, numFriends: Int): String {
val n = word.length
var ans = word.substring(0, 1)
fundfs(idx: Int, last: Int, split: MutableList<Int>) {
if (idx == numFriends-1) {
val cuts = mutableListOf(0); cuts.addAll(split); cuts.add(n)
for (i in0 until numFriends) {
val l = cuts[i]; val r = cuts[i+1]
val s = word.substring(l, r)
if (s.length > ans.length || (s.length == ans.length && s > ans)) ans = s
}
return }
for (i in last+1 until n-(numFriends-idx-1)) {
split.add(i)
dfs(idx+1, i, split)
split.removeAt(split.size-1)
}
}
dfs(0, 0, mutableListOf())
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
defget_max_string(word: str, numFriends: int) -> str:
n = len(word)
ans = word[0]
defdfs(idx: int, last: int, split: list[int]):
nonlocal ans
if idx == numFriends-1:
cuts = [0] + split + [n]
for i in range(numFriends):
l, r = cuts[i], cuts[i+1]
s = word[l:r]
if len(s) > len(ans) or (len(s) == len(ans) and s > ans):
ans = s
returnfor i in range(last+1, n-(numFriends-idx-1)):
dfs(idx+1, i, split+[i])
dfs(0, 0, [])
return ans