2025-06-04 Daily Challenge
Today I have done leetcode's June LeetCoding Challenge with cpp
.
June LeetCoding Challenge 4
Description
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.
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;
string_view w(word);
set<string_view> st;
int answerLen = word.length() - numFriends + 1;
for(int i = 0; i < word.length(); ++i) {
st.insert(w.substr(i, min<uint>(answerLen, word.length() - i)));
}
return string(*st.rbegin());
}
};
// Accepted
// 785/785 cases passed (79 ms)
// Your runtime beats 23.57 % of cpp submissions
// Your memory usage beats 83.84 % of cpp submissions (16.6 MB)