enter image description hereclass Solution {
public:
string mergeAlternately(string word1, string word2) {
int len1 = word1.length();
int len2 = word2.length();
string ans;
for (int i = 0, j = 0, k = 0; i < word1.length() + word2.length(); i++) {
if (i % 2 == 0 && j < len1 || k == len2) {
ans += word1[j];
j++;
}
if (i % 2 == 1 && k < len2 || j == len1) {
ans += word2[k];
k++;
}
}
return ans;
}
};
I checked if I am appending null character but it is less than Len so I have no idea what is wrong with the code.
New contributor
Faiyaz Fardin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.