Merge Strings Alternately
Real-world version: Braided Bead StrandsThe 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.
def braid_strands(strandA: str, strandB: str) -> str:public String braidStrands(String strandA, String strandB)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's