Skip to content

Practice Reverse String II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#541
LeetCode Problem

Reverse String II

Problem Statement

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 there are fewer than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and leave the other as original.

Source: Reverse String II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "abcdefg", k = 2
Output
"bacdfeg"

# Example 2

Input
s = "abcd", k = 2
Output
"bacd"

Constraints

1 <= s.length <= 10^4
s consists of only lowercase English letters.
1 <= k <= 10^4

Related LeetCode Problems

Practice more problems similar to Reverse String II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.