Given a string, reverse it interleavingly. Here's how to do it for abcdefghi
and abcdefghij
, 0-indexed:
- Separate the chars at even indices from the chars at odd indices:
a c e g i b d f h
a c e g i b d f h j - Reverse the chars at odd indices:
a c e g i h f d b
a c e g i j h f d b - Interleave into one string again:
ahcfedgbi
ajchefgdib
Rules
- You must support both even-length and odd-length strings.
- 0-index-wise, you must reverse the chars at odd indices, not even.
- 1-index-wise, of course, you must reverse the chars at even indices, not odd.
- Input will consist of printable ASCII (code points 32-126), no newlines.
- You can take input either as a string or as a list of chars (NOT 1-char strings). E.g.
String
/char[]
orchar*
are allowed, butString[]
/char[][]
orchar**
aren't.
Test cases
Input
Output
Hello, World!
HdlroW ,olle!
Hello World!
H!llooW rlde
ABCDEFGHIJKLMNOPQRSTUVWXYZ
AZCXEVGTIRKPMNOLQJSHUFWDYB
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
}"{$y&w(u*s,q.o0m2k4i6g8e:c<a>_@]B[DYFWHUJSLQNOPMRKTIVGXEZC\A^?`=b;d9f7h5j3l1n/p-r+t)v'x%z#|!~
P
P
AB
AB
xyz
xyz
For the empty string, return the empty string itself.