LeetCode: Merge Strings Alternately Solution

1/**
2 * @param {string} word1
3 * @param {string} word2
4 * @return {string}
5 */
6var mergeAlternately = function (word1, word2) {
7 word1 = word1.split("")
8 word2 = word2.split("")
9 let res = ""
10 while (word1.length && word2.length) {
11 res += word1.shift() + word2.shift()
12 }
13 if (word1.length) res += word1.join("")
14 if (word2.length) res += word2.join("")
15 return res
16}

Comments

Loading comments...

Tags

leetcode

string

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Map Of Highest Peak

Feb 21, 2021

HoningJS

Search Posts