Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string. If fewer than k characters are left, reverse all of them. If at least k but fewer than 2k characters are left, reverse the first k and leave the rest as they are.
Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string. If fewer than k characters are left, reverse all of them. If at least k but fewer than 2k characters are left, reverse the first k and leave the rest as they are.
Example 1:
Input: {"s":"abcdefg","k":2}
Output: bacdfeg
Constraints:
1≤s.length≤104
s consists of only lowercase English letters.
1≤k≤104
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"s":"abcdefg","k":2}
Split "abcdefg" into blocks of 2k = 4. In each block, reverse the first k = 2 characters and leave the next 2 alone.