-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_strings.java
More file actions
31 lines (30 loc) · 937 Bytes
/
Copy pathmerge_strings.java
File metadata and controls
31 lines (30 loc) · 937 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
class Solution {
public String mergeAlternately(String word1, String word2) {
String ans=""; int i,j,k;
// if(word1.length()<=word2.length())
// {
for( i=0,j=0,k=0;i<Math.min(word1.length(), word2.length())*2;i++)
{
if(i%2==0)
ans+=word1.charAt(k++);
else
ans+=word2.charAt(j++);
}
if(word1.length()<=word2.length())
ans+=word2.substring(j);
else if(word2.length()<word1.length())
ans+=word1.substring(k);
// }
// else{
// for( i=0,j=0,k=0;i<word2.length()*2;i++)
// {
// if(i%2==0)
// ans+=word1.charAt(k++);
// else
// ans+=word2.charAt(j++);
// }
// ans+=word1.substring(k);
// }
return ans;
}
}