146. Zip Two Words
A jeweller threads two strands of lettered beads onto one string. The strand a and the strand b are given as words. The jeweller takes beads alternately: first the leading bead of a, then the leading bead of b, then the next bead of a, then the next of b, and so on.
If one word runs out of beads first, all the remaining beads of the other word are added at the end in their original order. Given the strings a and b, return the merged string produced by this alternating process.
Example 1
- Input:
- a = "abc", b = "xyz"
- Output:
- "axbycz"
- Explanation:
The beads alternate a, x, b, y, c, z, giving the string axbycz.
Example 2
- Input:
- a = "hello", b = "hi"
- Output:
- "hheillo"
- Explanation:
Alternating gives h, h, e, i. The word hi is then used up, so the rest of hello (llo) is appended: hheillo.
Example 3
- Input:
- a = "a", b = "wxyz"
- Output:
- "awxyz"
- Explanation:
One bead from each word gives aw. The first word is exhausted, so the remaining xyz is appended: awxyz.
Constraints
1 ≤ a.length, b.length ≤ 100
a and b consist of lowercase English letters.
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n + m)
- Space
- O(n + m)