#541
Reverse String II
pupil · 495 · lc easy +27 · verified · 53.4% accepted · 2,342 likes · top 44%
Description
Given a string s and an integer k, process the string in non-overlapping blocks of 2k characters from left to right, reversing the first k characters of each block. In the final block: if fewer than k characters remain reverse all of them; if between k and 2k characters remain reverse only the first k and keep the rest unchanged.
Example 1:
Input: s = "abcdefg", k = 2
Output: "bacdfeg"
Example 2:
Input: s = "abcd", k = 2
Output: "bacd"
Code
1
2
3