-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode1768_mergeStrings.java
More file actions
35 lines (27 loc) · 961 Bytes
/
Copy pathleetcode1768_mergeStrings.java
File metadata and controls
35 lines (27 loc) · 961 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
30
31
32
33
34
35
public class leetcode1768_mergeStrings {
public String mergeAlternately(String word1, String word2) {
int word1Len = word1.length(), word2Len = word2.length(), count1=0, count2=0;
StringBuilder sb = new StringBuilder();
while(word1Len>0 && word2Len>0) {
sb.append(word1.charAt(count1++));
sb.append(word2.charAt(count2++));
word1Len--;
word2Len--;
}
while(word1Len > 0) {
sb.append(word1.charAt(count1++));
word1Len--;
}
while (word2Len > 0) {
sb.append(word2.charAt(count2++));
word2Len--;
}
return sb.toString();
}
public static void main(String[] args) {
leetcode1768_mergeStrings obj = new leetcode1768_mergeStrings();
String word1 = "abc", word2 = "pqr";
String res = obj.mergeAlternately(word1,word2);
System.out.println(res);
}
}