Skip to content

Commit

Permalink
Solve: Merge Strings Alternately
Browse files Browse the repository at this point in the history
  • Loading branch information
fkdl0048 authored Oct 9, 2024
1 parent e4b0cba commit 9fdbc0c
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions LeetCode/Merge Strings Alternately;.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
string mergeAlternately(string word1, string word2) {
string result;
int i = 0, j = 0;

while (i < word1.size() && j < word2.size()) {
result.push_back(word1[i++]);
result.push_back(word2[j++]);
}

while (i < word1.size()) {
result.push_back(word1[i++]);
}

while (j < word2.size()) {
result.push_back(word2[j++]);
}

return result;
}
};

0 comments on commit 9fdbc0c

Please sign in to comment.