Skip to content

Commit fd318bd

Browse files
committed
Update : PR Review comments addressed
1 parent d0f7ba7 commit fd318bd

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

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

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
class Solution {
22
public String mergeAlternately(String word1, String word2) {
3-
int i = 0, j = 0;
3+
int i = 0;
44
StringBuilder res = new StringBuilder();
55

6-
while (i < word1.length() && j < word2.length()) {
7-
res.append(word1.charAt(i));
8-
res.append(word2.charAt(j));
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+
}
913
i++;
10-
j++;
1114
}
1215

13-
res.append(word1.substring(i));
14-
res.append(word2.substring(j));
1516
return res.toString();
1617
}
17-
}
18+
}
19+
20+
/**
21+
* Time Complexity : O(n+m)
22+
* Space Complexity : O(n+m)
23+
*/

0 commit comments

Comments
 (0)