Skip to content

Commit 0585544

Browse files
Create largest_merge_of_two_strings.cpp
1 parent 8cd7f25 commit 0585544

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

largest_merge_of_two_strings.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
string largestMerge(string word1, string word2) {
4+
string ans="";
5+
while(!word1.empty() || !word2.empty()) {
6+
if(word1[0]>word2[0]) {
7+
ans+=word1[0];
8+
word1.erase(word1.begin());
9+
}else if(word1[0]<word2[0]) {
10+
ans+=word2[0];
11+
word2.erase(word2.begin());
12+
}else {
13+
if(word1<word2) {
14+
ans+=word2[0];
15+
word2.erase(word2.begin());
16+
}else {
17+
ans+=word1[0];
18+
word1.erase(word1.begin());
19+
}
20+
}
21+
}
22+
return ans;
23+
}
24+
};

0 commit comments

Comments
 (0)