We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a768c2c commit f9a8262Copy full SHA for f9a8262
Easy/Merge Strings Alternately.java
@@ -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