Skip to main content

Reverse String II - Solution & Explanation

EasyTwo PointersString14 min readAsked at: Amazon, Microsoft, Meta +4
Practice this problem

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.

 

Example 1:

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

Example 2:

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

 

Constraints:

  • 1 <= s.length <= 104
  • s consists of only lowercase English letters.
  • 1 <= k <= 104

Approach Overview

Problem Overview: You are given a string s and an integer k. For every block of 2k characters, reverse the first k characters and keep the next k characters unchanged. If fewer than k characters remain, reverse all of them. If between k and 2k remain, reverse the first k and leave the rest as is.

Approach 1: In-place String Modification (O(n) time, O(1) space)

This approach processes the string in chunks of size 2k. Start from index 0, then move forward in steps of 2k. For each block, reverse the first k characters using a two-pointer technique: one pointer at the start of the block and another at min(start + k - 1, n - 1). Swap characters while moving the pointers toward each other. This ensures only the required portion of each block is reversed.

The key insight is that every 2k window is independent. You never need to touch the second half of the block. Converting the string to a mutable character array allows swaps to happen directly without allocating additional memory. This makes the approach efficient with O(n) time since every character is processed at most once, and O(1) extra space.

This solution relies on simple index manipulation and the two pointers technique, which is common in string and array problems.

Approach 2: String Builder for Efficiency (O(n) time, O(n) space)

Another way is to construct the result using a string builder (or dynamic string). Iterate through the string in steps of 2k. For each iteration, take the first k characters, reverse them explicitly, and append them to the builder. Then append the next k characters in their original order.

This approach avoids modifying the original string and instead builds the answer sequentially. Reversing the substring can be done with a simple loop or a helper function. Because each character is appended exactly once, the total runtime remains O(n). However, the builder stores a new string, so the extra space usage becomes O(n).

This pattern is common in many string manipulation problems where immutability makes in-place edits inconvenient. It can also produce cleaner code in languages where strings cannot be modified directly.

Recommended for interviews: The in-place two-pointer solution is what interviewers typically expect. It demonstrates control over indexing and efficient string manipulation while maintaining O(n) time and O(1) space. The string builder approach still shows correct reasoning and is acceptable when code clarity matters, but the in-place method highlights stronger algorithmic discipline.

Approach 1: Approach 1: In-place String Modification

In this approach, we directly manipulate the string representation by considering it as a character array (or list in some languages) for in-place modification. We handle the string in chunks of 2k and reverse the first k characters of each chunk.

This C program utilizes a function reverseString which modifies the string s in place. It processes segments of length 2k, reversing the first k characters within each segment. The main function demonstrates an example with s = "abcdefg" and k = 2.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1), as we are reversing in place.

Try this approach in the editor →

Approach 2: Approach 2: String Builder for Efficiency

In this approach, we use a StringBuilder (or equivalent structure in the respective programming languages) to construct the final output. This approach is effective because handling mutable strings directly supports efficient character manipulation operations.

This C solution defines a helper function reverseSubString to serve reversal needs within the limits of given indices. The main logic within reverseString iterates over character chunks and utilizes this helper for targeted manipulation.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1) as we operate directly on the input array.

Try this approach in the editor →

Approach 3: Two Pointers

We can traverse the string s, iterating over every 2k characters, and then use the two-pointer technique to reverse the first k characters among these 2k characters.

The time complexity is O(n), and the space complexity is O(n). Here, n is the length of the string s.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: In-place String Modification

Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1), as we are reversing in place.

Approach 2: String Builder for Efficiency

Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1) as we operate directly on the input array.

Two Pointers—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
In-place String ModificationO(n)O(1)Best for interviews and memory‑efficient solutions where the string can be converted to a mutable array
String Builder for EfficiencyO(n)O(n)Useful when strings are immutable or when clearer step‑by‑step construction is preferred

Video Solution

Reverse String ii | LeetCode 541 | C++ • Knowledge Center • 17,782 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Reverse String II easy or hard?
Reverse String II is classified as an Easy problem. The challenge lies in carefully handling index boundaries and partial segments when fewer than k or 2k characters remain. Once the 2k iteration pattern is understood, the implementation is straightforward.
Reverse String II Python/Java solution
In Python, convert the string to a list of characters and reverse segments using index swaps. In Java, convert the string to a char array and apply the same two-pointer logic. Both implementations follow the same O(n) time algorithm with segment jumps of 2k.
How to solve Reverse String II in O(n)?
Process the string in intervals of 2k. For each starting index i, reverse the substring from i to min(i + k - 1, n - 1) using two pointers that swap characters inward. Then jump to i + 2k and repeat until the string ends.
What is the best approach for Reverse String II?
The most efficient approach is the in-place two-pointer method. Iterate through the string in steps of 2k and reverse only the first k characters of each block. This runs in O(n) time and uses O(1) extra space since swaps happen directly on a character array.
Is Reverse String II asked at Google/Amazon/Meta?
String manipulation and two-pointer problems like Reverse String II frequently appear in coding interviews at companies such as Amazon, Google, and Meta. While the exact question may vary, the pattern of reversing segments and handling edge cases is common in interview rounds.
What data structure is used in Reverse String II?
The main structure used is a mutable character array derived from the input string. The algorithm then applies the two-pointer technique to swap characters within the required range. Some implementations also use a string builder when constructing a new result string.
What is the time complexity of Reverse String II?
The time complexity is O(n), where n is the length of the string. Each character is visited at most once while reversing segments of length k in every 2k block. No nested loops scale with n, so the runtime remains linear.

Ready to solve this problem?

Practice Reverse String II with our built-in code editor and test cases.

Practice on FleetCode