|
| 1 | +--- |
| 2 | +id: reverse-string-ii |
| 3 | +title: Reverse String II |
| 4 | +sidebar_label: 0541 - Reverse String II |
| 5 | +tags: |
| 6 | + - String |
| 7 | + - Two Pointers |
| 8 | + - Recursion |
| 9 | +description: "This is a solution to the Reverse String II problem on LeetCode." |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +Given a string `s` and an integer `k`, reverse the first `k` characters for every `2k` characters counting from the start of the string. |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +### Examples |
| 19 | + |
| 20 | +**Example 1:** |
| 21 | + |
| 22 | +``` |
| 23 | +Input: s = "abcdefg", k = 2 |
| 24 | +Output: "bacdfeg" |
| 25 | +``` |
| 26 | + |
| 27 | +**Example 2:** |
| 28 | + |
| 29 | +``` |
| 30 | +Input: s = "abcd", k = 2 |
| 31 | +Output: "bacd" |
| 32 | +``` |
| 33 | + |
| 34 | +### Constraints |
| 35 | + |
| 36 | +- $1 \leq s.length \leq 10^4$ |
| 37 | +- `s` consists of only lowercase English letters. |
| 38 | +- $1 \leq k \leq 10^4$ |
| 39 | + |
| 40 | +## Solution for Reverse String II |
| 41 | + |
| 42 | +### Approach |
| 43 | +#### Intuition and Algorithm |
| 44 | + |
| 45 | +We will reverse each block of 2k characters directly. |
| 46 | + |
| 47 | +Each block starts at a multiple of 2k: for example, 0, 2k, 4k, 6k, .... One thing to be careful about is we may not reverse each block if there aren't enough characters. |
| 48 | + |
| 49 | +To reverse a block of characters from i to j, we can swap characters in positions i++ and j--. |
| 50 | + |
| 51 | +## Code in Different Languages |
| 52 | + |
| 53 | +<Tabs> |
| 54 | +<TabItem value="cpp" label="C++"> |
| 55 | + <SolutionAuthor name="@Shreyash3087"/> |
| 56 | + |
| 57 | +```cpp |
| 58 | +#include <iostream> |
| 59 | +#include <string> |
| 60 | +#include <algorithm> |
| 61 | + |
| 62 | +class Solution { |
| 63 | +public: |
| 64 | + std::string reverseStr(std::string s, int k) { |
| 65 | + for (int start = 0; start < s.length(); start += 2 * k) { |
| 66 | + int i = start; |
| 67 | + int j = std::min(start + k - 1, static_cast<int>(s.length()) - 1); |
| 68 | + while (i < j) { |
| 69 | + std::swap(s[i++], s[j--]); |
| 70 | + } |
| 71 | + } |
| 72 | + return s; |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +``` |
| 77 | +</TabItem> |
| 78 | +<TabItem value="java" label="Java"> |
| 79 | + <SolutionAuthor name="@Shreyash3087"/> |
| 80 | +
|
| 81 | +```java |
| 82 | +class Solution { |
| 83 | + public String reverseStr(String s, int k) { |
| 84 | + char[] a = s.toCharArray(); |
| 85 | + for (int start = 0; start < a.length; start += 2 * k) { |
| 86 | + int i = start, j = Math.min(start + k - 1, a.length - 1); |
| 87 | + while (i < j) { |
| 88 | + char tmp = a[i]; |
| 89 | + a[i++] = a[j]; |
| 90 | + a[j--] = tmp; |
| 91 | + } |
| 92 | + } |
| 93 | + return new String(a); |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +</TabItem> |
| 99 | +<TabItem value="python" label="Python"> |
| 100 | + <SolutionAuthor name="@Shreyash3087"/> |
| 101 | + |
| 102 | +```python |
| 103 | +class Solution(object): |
| 104 | + def reverseStr(self, s, k): |
| 105 | + a = list(s) |
| 106 | + for i in xrange(0, len(a), 2*k): |
| 107 | + a[i:i+k] = reversed(a[i:i+k]) |
| 108 | + return "".join(a) |
| 109 | +``` |
| 110 | +</TabItem> |
| 111 | +</Tabs> |
| 112 | + |
| 113 | +## Complexity Analysis |
| 114 | + |
| 115 | +### Time Complexity: $O(N)$ |
| 116 | + |
| 117 | +> **Reason**: where N is the size of `s`. We build a helper array, plus reverse about half the characters in `s`. |
| 118 | + |
| 119 | +### Space Complexity: $O(N)$ |
| 120 | + |
| 121 | +> **Reason**: the size of `a`. |
| 122 | + |
| 123 | +## References |
| 124 | + |
| 125 | +- **LeetCode Problem**: [Reverse String II](https://leetcode.com/problems/reverse-string-ii/description/) |
| 126 | + |
| 127 | +- **Solution Link**: [Reverse String II](https://leetcode.com/problems/reverse-string-ii/solutions/) |
0 commit comments