-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeAlternately.java
More file actions
29 lines (23 loc) · 857 Bytes
/
Copy pathMergeAlternately.java
File metadata and controls
29 lines (23 loc) · 857 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package LeetcodePractice;
public class MergeAlternately {
public static String mergeAlternately(String word1, String word2) {
StringBuilder result = new StringBuilder();
int i = 0, j = 0;
while (i < word1.length() && j < word2.length()) {
result.append(word1.charAt(i++));
result.append(word2.charAt(j++));
}
while (i < word1.length()) {
result.append(word1.charAt(i++));
}
while (j < word2.length()) {
result.append(word2.charAt(j++));
}
return result.toString();
}
public static void main(String[] args) {
System.out.println(mergeAlternately("abc", "pqr"));
System.out.println(mergeAlternately("ab", "pqrs"));
System.out.println(mergeAlternately("abcd", "pq"));
}
}