← All problems

1. Merge Strings Alternately

EasyArray / StringOriginal on LeetCode ↗

You are given two strings, word1 and word2. Build a new string by taking characters in turn: the first character of word1, then the first of word2, then the second of word1, and so on.

If one string is longer, copy whatever is left of it onto the end once the shorter one is used up. Return the combined string.

Constraints

  • 1 <= len(word1), len(word2) <= 100
  • Both strings contain only lowercase English letters.

Examples

Example 1
Input:  word1 = "cat", word2 = "dog"
Output: "cdaotg"
Example 2
Input:  word1 = "ab", word2 = "wxyz"
Output: "awbxyz"
Example 3
Input:  word1 = "hello", word2 = "io"
Output: "hieollo"

Hints

Hint 1

Walk both strings with one index and append a character from each while both still have characters.

Hint 2

When one string runs out, the rest of the other one goes on the end unchanged.

Python 3

Run checks the examples · Submit checks every test. The first run downloads Python (about 10 MB) and takes a few seconds.