Skip to content

Commit 59df090

Browse files
authored
Create Merge_String_Alternately.java
1 parent e0dcab9 commit 59df090

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public String mergeAlternately(String word1, String word2) {
3+
String s = "";
4+
int m = word2.length();
5+
int n= word1.length();
6+
int l = m>n?n:m;
7+
int i=0;
8+
for(;i<l;i++){
9+
char ch = word1.charAt(i);
10+
s=s+ch;
11+
m--;
12+
char ch2 = word2.charAt(i);
13+
s=s+ch2;
14+
n--;
15+
}
16+
if(m>0){
17+
s=s+word2.substring(i);
18+
}else{
19+
s=s+word1.substring(i);
20+
}
21+
return s;
22+
}
23+
24+
25+
}
26+
Footer

0 commit comments

Comments
 (0)