2023-04-19 00:17:54 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
class Solution {
|
2024-01-03 12:06:42 +01:00
|
|
|
public:
|
|
|
|
std::string mergeAlternately(const std::string &word1,
|
|
|
|
const std::string &word2) {
|
2023-04-19 00:17:54 +02:00
|
|
|
std::string result;
|
|
|
|
|
|
|
|
auto l = word1.begin();
|
|
|
|
auto r = word2.begin();
|
|
|
|
|
|
|
|
for (; l != word1.end() && r != word2.end(); ++l, ++r) {
|
|
|
|
result += *l;
|
|
|
|
result += *r;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; l != word1.end(); ++l) {
|
|
|
|
result += *l;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; r != word2.end(); ++r) {
|
|
|
|
result += *r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
int main() {
|
2023-04-19 00:17:54 +02:00
|
|
|
Solution s;
|
|
|
|
|
|
|
|
assert(s.mergeAlternately("abc", "pqr") == "apbqcr");
|
|
|
|
assert(s.mergeAlternately("ab", "pqrs") == "apbqrs");
|
|
|
|
assert(s.mergeAlternately("abcd", "pq") == "apbqcd");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|