1768. Merge Strings Alternately

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public String mergeAlternately(String word1, String word2) {
StringBuilder sb = new StringBuilder();

int i = 0, j = 0;
while (i < word1.length() || j < word2.length()) {
if (i < word1.length()) {
sb.append(word1.charAt(i++));
}
if (j < word2.length()) {
sb.append(word2.charAt(j++));
}
}

return sb.toString();
}
}

References

1768. Merge Strings Alternately