3403. 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 intonumFriends
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.
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.
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 * 103
word
consists only of lowercase English letters.1 <= numFriends <= word.length
Solution:
class Solution {
public String answerString(String word, int numFriends) {
if (numFriends == 1){
return word;
}
int n = word.length();
String max = "";
for (int i = 0; i < n; i++){
String t = word.substring(i, Math.min(n, n - (numFriends - 1 - i)));
if (t.compareTo(max) > 0){
max = t;
}
}
return max;
}
}
Dfs上头了
啥都感觉要dfs一下
class Solution {
public String answerString(String word, int n) {
if(n == 1) {
return word;
}
PriorityQueue<String> pq = new PriorityQueue<>(Collections.reverseOrder());
for(int i = 0;i < word.length();i++) {
int x = word.length()-(n-1);
x = i+x;
// System.out.println(i+" "+x+" "+word.substring(i, x <= word.length() ? x : word.length()));
pq.add(word.substring(i, x <= word.length() ? x : word.length()));
}
return pq.peek();
}
}