Skip to content

Commit 695011c

Browse files
committed
add 1768 ts solution
1 parent e5d3654 commit 695011c

File tree

1 file changed

+18
-0
lines changed
  • Problems/22-Merge-Strings-Alternately

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function mergeAlternately(word1: string, word2: string): string {
2+
let ptr1: number = 0;
3+
let ptr2: number = 0;
4+
const len1 = word1.length;
5+
const len2 = word2.length;
6+
let res: string[] = [];
7+
while (ptr1 < len1 || ptr2 < len2) {
8+
if (ptr1 < len1 && ptr2 < len2) {
9+
res.push(word1[ptr1++]);
10+
res.push(word2[ptr2++]);
11+
} else if (ptr1 > len1 || ptr2 < len2) {
12+
res.push(word2[ptr2++]);
13+
} else if (ptr1 < len1 || ptr2 > len2) {
14+
res.push(word1[ptr1++]);
15+
}
16+
}
17+
return res.join("");
18+
}

0 commit comments

Comments
 (0)