Reverse String II
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
1 <= len(s) <= 1001 <= k <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.