Show problems as
Classic problem #1768EasyTwo PointersString

Merge Strings Alternately

Real-world version: Braided Bead Strands
Tracked in this browser only
Read Merge Strings Alternately on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A jeweller braids two bead strands onto a single cord. Each strand arrives as text with one letter per bead, listed from the clasp outward: strandA and strandB.

The braid grows one bead at a time. First the clasp-most bead of strandA goes on, then the clasp-most bead of strandB, then the second bead of strandA, then the second bead of strandB, and the turns keep alternating like that. The moment one strand has nothing left to give, every bead still waiting on the other strand goes on in its own order and finishes the braid.

Return the finished braid as text.

Examples

Example 1

Input
strandA = "ruby", strandB = "jade"
Output
"rjuabdye"

Both strands hold four beads, so the turns alternate the whole way and neither strand has a leftover.

Example 2

Input
strandA = "opal", strandB = "tin"
Output
"otpianl"

Six beads alternate, then `strandB` is empty and the last bead of `strandA` closes the braid.

Example 3

Input
strandA = "gem", strandB = "amber"
Output
"gaemmber"

After six alternating beads `strandA` runs dry, so the two beads still on `strandB` follow in order.

Constraints

  • 1 <= strandA.length, strandB.length <= 100
  • strandA and strandB hold lowercase letters only

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def braid_strands(strandA: str, strandB: str) -> str:
Java
public String braidStrands(String strandA, String strandB)