LeetCode/java/merge-strings-alternately.java
Matej Focko f0cdb67ac1
chore(java): format
Signed-off-by: Matej Focko <mfocko@redhat.com>
2024-03-02 21:01:53 +01:00

20 lines
501 B
Java

class Solution {
public String mergeAlternately(String word1, String word2) {
StringBuilder sb = new StringBuilder(word1.length() + word2.length());
int i, j;
for (i = 0, j = 0; i < word1.length() && j < word2.length(); ++i, ++j) {
sb.append(word1.charAt(i));
sb.append(word2.charAt(j));
}
if (i < word1.length()) {
sb.append(word1.substring(i));
}
if (j < word2.length()) {
sb.append(word2.substring(j));
}
return sb.toString();
}
}