Problems > Reverse Every Other Chunk

Reverse Every Other Chunk

Difficulty: 1200 2000 ms 128 MB

strings

Split S into blocks of 2K; in each block reverse the FIRST K characters and keep the next K as they are (short tails: reverse what exists of the first K).

Input: the first line has S; the second line has K.

Output: the transformed string.

Constraints:

  • 1 ≤ |S| ≤ 104
  • 1 ≤ K ≤ |S|

Sample Tests

Input 1
abcdefg
2
Output 1
bacdfeg
Input 2
abcd
4
Output 2
dcba

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.