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 <= 104s consists of only lowercase English letters.1 <= k <= 104Problem 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.
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.
Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1), as we are reversing in place.
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.
Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1) as we operate directly on the input array.
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.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Approach 1: In-place String Modification | Time Complexity: O(n), where n is the length of the string. |
| Approach 2: String Builder for Efficiency | Time Complexity: O(n), where n is the length of the string. |
| Two Pointers | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| In-place String Modification | O(n) | O(1) | Best for interviews and memory‑efficient solutions where the string can be converted to a mutable array |
| String Builder for Efficiency | O(n) | O(n) | Useful when strings are immutable or when clearer step‑by‑step construction is preferred |
Reverse String ii | LeetCode 541 | C++ • Knowledge Center • 17,288 views views
Watch 9 more video solutions →Practice Reverse String II with our built-in code editor and test cases.
Practice on FleetCode