mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 01:49:41 +01:00
problems(cpp): add “1768. Merge Strings Alternately”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
d8009b9a4a
commit
748624e5ee
1 changed files with 39 additions and 0 deletions
39
problems/merge-strings-alternately.cpp
Normal file
39
problems/merge-strings-alternately.cpp
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
#include <cassert>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
std::string mergeAlternately(const std::string& word1, const std::string& word2)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
Solution s;
|
||||||
|
|
||||||
|
assert(s.mergeAlternately("abc", "pqr") == "apbqcr");
|
||||||
|
assert(s.mergeAlternately("ab", "pqrs") == "apbqrs");
|
||||||
|
assert(s.mergeAlternately("abcd", "pq") == "apbqcd");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in a new issue