Easy

Quiz

#541 Reverse String II

APPROACH

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"
1 of 4
1:00

What is the optimal approach for this problem?