Merge Strings Alternately
Easy · two pointers
Two friends are writing a message together, one letter at a time. You are given two strings, word1 and word2.
Build a new string by taking letters from the two strings in turn: the first letter of word1, then the first letter of word2, then the second letter of word1, then the second letter of word2, and so on.
If one string runs out of letters first, add all the remaining letters of the other string to the end. Return the merged string.
Examples
Example 1
Input: word1 = "abc", word2 = "xyz"
Output: "axbycz"
Example 2
Input: word1 = "ab", word2 = "pqrs"
Output: "apbqrs"
After a, p, b and q the first string is used up, so the rest of the second string, r and s, follows.
Example 3
Input: word1 = "hello", word2 = "w"
Output: "hwello"
Constraints
- 1 <= len(word1), len(word2) <= 1000
- Both strings contain only lower-case English letters.