FunctionRacerProblemsRace

Reverse String II

EasyStringsPython, JavaScript, TypeScript, Java, C++

Cut s into blocks of 2 * k characters, starting from the left. In every block, reverse the first k characters and leave the other k alone.

If the last block has fewer than k characters, reverse all of them. If it has between k and 2 * k characters, reverse only the first k.

Example 1

s "abcdefg"
k 2
returns "bacdfeg"

Blocks abcd, efg: bacd then feg, giving bacdfeg.

Example 2

s "abcd"
k 5
returns "dcba"

Fewer than k characters, so all are reversed.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Strings problems