Skip to main content

Reverse String II - Video Solutions

EasyTwo PointersString

Reverse String ii | LeetCode 541 | C++

Knowledge Center
5:3417,782 views
10 video solutions available

Reverse String II - Video Solution

Watch 10 video solutions for Reverse String II, a easy level problem involving Two Pointers, String. This walkthrough by Knowledge Center has 17,782 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

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
Read full problem with examples

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.

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