1768.cpp (423B)
1 class Solution { 2 public: 3 string mergeAlternately(string word1, string word2) { 4 int i = 0, j = 0; 5 string res = ""; 6 7 while (i < word1.size() && j < word2.size()) { 8 res += word1[i++]; 9 res += word2[j++]; 10 } 11 12 while (i < word1.size()) 13 res += word1[i++]; 14 while (j < word2.size()) 15 res += word2[j++]; 16 17 return res; 18 } 19 };