-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1768. Merge Strings Alternately
46 lines (42 loc) · 1.04 KB
/
1768. Merge Strings Alternately
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
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public:
string mergeAlternately(string word1, string word2) {
int n=word1.size();
int m=word2.size();
string s="";
int i=0;
int j=0;
while(n>i || m>j )
{
if(i!=n)
{s=s+word1[i];
i++;
}
if(j!=m)
{s=s+word2[j];
j++;
}
}
return s;
/////////////////////////////// another aproach of merge sort like that
// int n = word1.length();
// int m = word2.length();
// int i = 0, j = 0;
// string ans = "";
// while(i < n && j < m) {
// ans += word1[i];
// ans += word2[j];
// i++;
// j++;
// }
// while(i < n) {
// ans += word1[i];
// i++;
// }
// while(j < m) {
// ans += word2[j];
// j++;
// }
// return ans;
}
};