Skip to content

Commit 06b6d77

Browse files
authored
Merge pull request #2690 from AHTHneeuhl/1768
2 parents 655f90c + d4dee29 commit 06b6d77

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(n)
3+
4+
var mergeAlternately = function (word1, word2) {
5+
const buffer = [];
6+
7+
for (let i = 0; i < word1.length || i < word2.length; i++) {
8+
if (i < word1.length) buffer.push(word1[i]);
9+
if (i < word2.length) buffer.push(word2[i]);
10+
}
11+
12+
return buffer.join('');
13+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(n)
3+
4+
function mergeAlternately(word1: string, word2: string): string {
5+
const buffer: Array<string> = [];
6+
7+
for (let i = 0; i < word1.length || i < word2.length; i++) {
8+
if (i < word1.length) buffer.push(word1[i]);
9+
if (i < word2.length) buffer.push(word2[i]);
10+
}
11+
12+
return buffer.join('');
13+
}

0 commit comments

Comments
 (0)