Skip to content

Commit 8c632f3

Browse files
authored
Create largest-merge-of-two-strings.cpp
1 parent 0298dc2 commit 8c632f3

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

C++/largest-merge-of-two-strings.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time: O(n * m)
2+
// Space: O(n + m)
3+
4+
class Solution {
5+
public:
6+
string largestMerge(string word1, string word2) {
7+
queue<char> q1, q2;
8+
for (const auto& c : word1) {
9+
q1.emplace(c);
10+
}
11+
for (const auto& c : word2) {
12+
q2.emplace(c);
13+
}
14+
string result;
15+
while (!empty(q1) || !empty(q2)) {
16+
if (q1 < q2) {
17+
swap(q1, q2);
18+
}
19+
result.push_back(q1.front()); q1.pop();
20+
}
21+
return result;
22+
}
23+
};

0 commit comments

Comments
 (0)