Skip to content

Docs: Added Solutions to Leetcode 541 #1268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dsa-problems/leetcode-problems/0500-0599.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export const problems =[
"problemName": "541. Reverse String II",
"difficulty": "Easy",
"leetCodeLink": "https://leetcode.com/problems/reverse-string-ii",
"solutionLink": "#"
"solutionLink": "/dsa-solutions/lc-solutions/0500-0599/reverse-string-ii"
},
{
"problemName": "542. 01 Matrix",
Expand Down
127 changes: 127 additions & 0 deletions dsa-solutions/lc-solutions/0500-0599/0541-reverse-string-ii.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
---
id: reverse-string-ii
title: Reverse String II
sidebar_label: 0541 - Reverse String II
tags:
- String
- Two Pointers
- Recursion
description: "This is a solution to the Reverse String II problem on LeetCode."
---

## Problem Description

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.

### Examples

**Example 1:**

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

**Example 2:**

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

### Constraints

- $1 \leq s.length \leq 10^4$
- `s` consists of only lowercase English letters.
- $1 \leq k \leq 10^4$

## Solution for Reverse String II

### Approach
#### Intuition and Algorithm

We will reverse each block of 2k characters directly.

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.

To reverse a block of characters from i to j, we can swap characters in positions i++ and j--.

## Code in Different Languages

<Tabs>
<TabItem value="cpp" label="C++">
<SolutionAuthor name="@Shreyash3087"/>

```cpp
#include <iostream>
#include <string>
#include <algorithm>

class Solution {
public:
std::string reverseStr(std::string s, int k) {
for (int start = 0; start < s.length(); start += 2 * k) {
int i = start;
int j = std::min(start + k - 1, static_cast<int>(s.length()) - 1);
while (i < j) {
std::swap(s[i++], s[j--]);
}
}
return s;
}
};

```
</TabItem>
<TabItem value="java" label="Java">
<SolutionAuthor name="@Shreyash3087"/>

```java
class Solution {
public String reverseStr(String s, int k) {
char[] a = s.toCharArray();
for (int start = 0; start < a.length; start += 2 * k) {
int i = start, j = Math.min(start + k - 1, a.length - 1);
while (i < j) {
char tmp = a[i];
a[i++] = a[j];
a[j--] = tmp;
}
}
return new String(a);
}
}
```

</TabItem>
<TabItem value="python" label="Python">
<SolutionAuthor name="@Shreyash3087"/>

```python
class Solution(object):
def reverseStr(self, s, k):
a = list(s)
for i in xrange(0, len(a), 2*k):
a[i:i+k] = reversed(a[i:i+k])
return "".join(a)
```
</TabItem>
</Tabs>

## Complexity Analysis

### Time Complexity: $O(N)$

> **Reason**: where N is the size of `s`. We build a helper array, plus reverse about half the characters in `s`.

### Space Complexity: $O(N)$

> **Reason**: the size of `a`.

## References

- **LeetCode Problem**: [Reverse String II](https://leetcode.com/problems/reverse-string-ii/description/)

- **Solution Link**: [Reverse String II](https://leetcode.com/problems/reverse-string-ii/solutions/)
Loading