Skip to content

Commit 791f819

Browse files
authored
Merge pull request #2713 from Aakriti-malla/main
2 parents 0dbde16 + fd318bd commit 791f819

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Diff for: java/1768-merge-strings-alternately.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public String mergeAlternately(String word1, String word2) {
3+
int i = 0;
4+
StringBuilder res = new StringBuilder();
5+
6+
while (i < word1.length() || i < word2.length()) {
7+
if (i < word1.length()) {
8+
res.append(word1.charAt(i));
9+
}
10+
if (i < word2.length()) {
11+
res.append(word2.charAt(i));
12+
}
13+
i++;
14+
}
15+
16+
return res.toString();
17+
}
18+
}
19+
20+
/**
21+
* Time Complexity : O(n+m)
22+
* Space Complexity : O(n+m)
23+
*/

0 commit comments

Comments
 (0)