AlgoLounge

Merge Strings Alternately

medium

You are given two strings, word1 and word2. Construct a new string by merging them in alternating order, starting with word1 — take one character from word1, then one from word2, and repeat this process.

If one string is longer than the other, append the remaining characters from the longer string to the end of the merged result.

Return the final merged string.

Examples

Example 1

Input:  word1 = "abc", word2 = "xyz"
Output: "axbycz"

Example 2

Input:  word1 = "ab", word2 = "abbxxc"
Output: "aabbbxxc"

Constraints

  • 1 <= word1.length, word2.length <= 100
  • word1 and word2 consist of lowercase English letters.
TypeScript
Loading editor…
List of test cases (5)
Case 1
word1 = "abc"
word2 = "xyz"
Case 2
word1 = "ab"
word2 = "abbxxc"
Case 3
word1 = "a"
word2 = "b"
Case 4
word1 = "hello"
word2 = "world"
Case 5
word1 = "x"
word2 = "yzabc"