You are given two strings word1
and word2
. You want to construct a string merge
in the following way: while either word1
or word2
are non-empty, choose one of the following options:
- If
word1
is non-empty, append the first character inword1
tomerge
and delete it fromword1
.- For example, if
word1 = "abc"
andmerge = "dv"
, then after choosing this operation,word1 = "bc"
andmerge = "dva"
.
- For example, if
- If
word2
is non-empty, append the first character inword2
tomerge
and deleteg it fromword2
.- For example, if
word2 = "abc"
andmerge = ""
, then after choosing this operation,word2 = "bc"
andmerge = "a"
.
- For example, if
Return the lexicographically largest merge
you can construct.
A string a
is lexicographically larger than a string b
(of the same length) if in the first position where a
and b
differ, a
has a character strictly larger than the corresponding character in b
. For example, "abcd"
is lexicographically larger than "abcc"
because the first position they differ is at the fourth character, and d
is greater than c
.
Example 1:
Input: word1 = "cabaa", word2 = "bcaaa" Output: "cbcabaaaaa" Explanation: One way to get the lexicographically largest merge is: - Take from word1: merge = "c", word1 = "abaa", word2 = "bcaaa" - Take from word2: merge = "cb", word1 = "abaa", word2 = "caaa" - Take from word2: merge = "cbc", word1 = "abaa", word2 = "aaa" - Take from word1: merge = "cbca", word1 = "baa", word2 = "aaa" - Take from word1: merge = "cbcab", word1 = "aa", word2 = "aaa" - Append the remaining 5 a's from word1 and word2 at the end of merge.
Example 2:
Input: word1 = "abcabc", word2 = "abdcaba" Output: "abdcabcabcaba"
Constraints:
1 <= word1.length, word2.length <= 3000
word1
andword2
consist only of lowercase English letters.
题目:给定两个字符串,每次选取两个字符串最前位置的字符,组合成一个新的字符串,使新组成的字符串最大。
思路:这是一道周赛题,刚好我那周参加了。用两个指针分别指向两个字符串最前位置,比较两个字符大小,分三种情况:
1,如果word1[i]大,则选取word1[i], 更新结果,更新指针i;
2,反之如果word2[j]大,则选取word2[j],更新结果更新指针j.
3,难点在如果word1[i] == word2[j],则定义新指针i1, j1一直往后找,找到第一个不相等的字符。这里又分为3种情况:
(1) i1找到了字符串末尾,则选取字符串word2第一个字符,更新指针j,
(2) j1找到了字符串末尾,则选取字符串word1第一个字符,更新指针i;
(3) i1和j1都每到字符串末尾,则比较word1[i1]和word2[j1]。
最后记得如果一个字符串选择完了,需将另一个字符串剩余的子字符串附加在结果后。
代码如下:
class Solution {
public:
string largestMerge(string word1, string word2) {
string res = "";
int i = 0, j = 0;
while(i < word1.length() && j < word2.length()){
if(word1[i] > word2[j]){
res.push_back(word1[i++]);
} else if(word1[i] < word2[j]){
res.push_back(word2[j++]);
} else {
int i1 = i, j1 = j;
while(i1 < word1.length() && j1 < word2.length() && word1[i1] == word2[j1]){
i1++;
j1++;
}
if(i1 == word1.length()) res.push_back(word2[j++]);
else if(j1 == word2.length()) res.push_back(word1[i++]);
else {
if(word1[i1] > word2[j1]){
res.push_back(word1[i++]);
} else{
res.push_back(word2[j++]);
}
}
}
}
if(i < word1.length()) res += word1.substr(i);
if(j < word2.length()) res += word2.substr(j);
return res;
}
};
time: O(N), space:O(1).
word1[i] == word2[j] 的时候三种情况可以优化合并一下,直接比较word1.substr(i) 和word2.substr(j) 即可。这种虽然代码优化了,但增加了时间复杂度:
class Solution {
public:
string largestMerge(string word1, string word2) {
string res = "";
int i = 0, j = 0;
while(i < word1.length() && j < word2.length()){
if(word1[i] > word2[j]){
res.push_back(word1[i++]);
} else if(word1[i] < word2[j]){
res.push_back(word2[j++]);
} else {
if(word1.substr(i) >= word2.substr(j))
res.push_back(word1[i++]);
else
res.push_back(word2[j++]);
}
}
if(i < word1.length()) res += word1.substr(i);
if(j < word2.length()) res += word2.substr(j);
return res;
}
};