Reverse String II - Problem
Reverse String II is a fascinating string manipulation problem that challenges you to selectively reverse portions of a string based on a specific pattern.

Given a string s and an integer k, you need to process the string in chunks of 2k characters. For each chunk:

Reverse the first k characters of every 2k-character segment
• If fewer than k characters remain, reverse all remaining characters
• If between k and 2k-1 characters remain, reverse only the first k characters

This problem tests your understanding of string indexing, boundary conditions, and efficient character manipulation techniques.

Example: With s = "abcdefg" and k = 2, you'd reverse "ab" (first 2), keep "cd" (next 2), then reverse "efg" (remaining < k, so reverse all) → "bacdfeg"

Input & Output

example_1.py — Basic Case
$ Input: s = "abcdefg", k = 2
Output: "bacdfeg"
💡 Note: First 2k=4 characters: reverse "ab" → "ba", keep "cd". Remaining 3 characters (< k=2): reverse all "efg" → "gfe". But since we have exactly 3 chars and k=2, we reverse first 2: "ef" → "fe", keep "g". Wait, let me recalculate: we have "efg" (3 chars), which is > k but < 2k, so reverse first k=2 chars: "ef" → "fe", keep "g" → "feg". Actually, since 3 < k, we reverse all: "efg" → "gfe". Final: "ba" + "cd" + "gfe" = "bacdgfe". Let me recalculate properly: "efg" has 3 chars, k=2, so reverse first min(k, remaining) = min(2,3) = 2 chars: "ef" → "fe", keep "g" → "feg". But the rule says if < k chars remain, reverse all. Since 3 > k=2, we reverse first k=2: "fe" + "g" = "feg". Hmm, let me check: remaining = "efg" (3 chars), k=2. Since 3 ≥ k, reverse first k chars: "ef" → "fe", result: "fe" + "g" = "feg". So "ba" + "cd" + "feg" = "bacdfeg".
example_2.py — Exact 2k Length
$ Input: s = "abcd", k = 2
Output: "bacd"
💡 Note: String length is exactly 2k=4. First chunk: reverse first k=2 characters "ab" → "ba", keep next k=2 characters "cd". Result: "ba" + "cd" = "bacd".
example_3.py — Single Character
$ Input: s = "a", k = 1
Output: "a"
💡 Note: Only one character remaining, which is < k, so reverse all (just "a"). Since reversing a single character doesn't change it, result is "a".

Constraints

  • 1 ≤ s.length ≤ 104
  • s consists of only lowercase English letters
  • 1 ≤ k ≤ 104
  • k can be larger than string length

Visualization

Tap to expand
String Reversal Pattern VisualizationBookshelf Sections (k=2, so 2k=4 per section)Section 1abcdReverse theseKeep theseRemaining (< 2k)efg3 chars ≥ k=2, so reverse first kFinal Result: "bacdfeg"bacdfeg
Understanding the Visualization
1
Identify sections
Divide the string into sections of 2k characters each
2
Reverse first half
In each complete section, reverse only the first k characters
3
Handle remainder
For incomplete sections, apply the appropriate rule based on remaining length
4
Combine result
Join all processed sections to form the final string
Key Takeaway
🎯 Key Insight: Process the string in chunks of 2k characters, using two pointers to efficiently reverse only the first k characters of each chunk in-place, achieving optimal O(n) time with O(1) extra space.
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 6
28.6K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen