-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1768_MergeAlternately.cpp
More file actions
41 lines (36 loc) · 1014 Bytes
/
Copy path1768_MergeAlternately.cpp
File metadata and controls
41 lines (36 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <fmt/ranges.h>
#include <iostream>
#include <string>
#include <utility>
using namespace std;
class Solution
{
public:
string mergeAlternately(string word1, string word2)
{
auto it1 = word1.begin();
auto it2 = word2.begin();
while (it2 != word2.end()) {
if (it1 != word1.end())
++it1;
it1 = word1.insert(it1, *it2);
if (it1 != word1.end())
++it1;
++it2;
}
return word1;
}
};
int main()
{
Solution sol;
// test cases
std::pair<string, string> test1 = {"abc", "pqr"};
std::pair<string, string> test2 = {"ab", "pqrs"};
std::pair<string, string> test3 = {"abcd", "pq"};
std::pair<string, string> test4 = {"dajmknzgidixqgt", "nahamebx"};
for (auto test : {test4, test1, test2, test3}) {
auto result = sol.mergeAlternately(test.first, test.second);
fmt::print("Before: {} {} After: {}\n", test.first, test.second, result);
}
}