Skip to content

Commit f9a8262

Browse files
authored
Added Merge Strings Alternately.java
1 parent a768c2c commit f9a8262

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Easy/Merge Strings Alternately.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public String mergeAlternately(String word1, String word2) {
3+
int idx1 = 0;
4+
int idx2 = 0;
5+
StringBuilder sb = new StringBuilder();
6+
boolean first = true;
7+
while (idx1 < word1.length() && idx2 < word2.length()) {
8+
sb.append(first ? word1.charAt(idx1++) : word2.charAt(idx2++));
9+
first = !first;
10+
}
11+
while (idx1 < word1.length()) {
12+
sb.append(word1.charAt(idx1++));
13+
}
14+
while (idx2 < word2.length()) {
15+
sb.append(word2.charAt(idx2++));
16+
}
17+
return sb.toString();
18+
}
19+
}

0 commit comments

Comments
 (0)