All problems

Problem 829

Reverse String II

Easy · 10 pts · String, Two Pointers

Problem

Split the string into consecutive chunks of 2K characters (the last chunk may be shorter). In every chunk, reverse the first K characters and leave the rest as they are. If a chunk has fewer than K characters, reverse all of them. Print the resulting string. Walk through the string with a step of 2K and reverse the range from i to min(i + K, length) - 1 each time.

Input

The first line contains S (lowercase letters). The second line contains K.

Output

Print the transformed string.

Constraints

1 <= |S| <= 100000; 1 <= K <= 100000

Example input

abcdefg
2

Example output

bacdfeg

Explanation

For abcdefg with K = 2, the chunks are abcd, efg. Reversing the first two letters of each gives bacd and feg, so the answer is bacdfeg.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Kids With the Greatest Number of Candies Next: Reverse Only Letters