## Algorithm

Problem Name: 541. Reverse String II

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`

## Code Examples

### #1 Code Example with C++ Programming

```Code - C++ Programming```

``````
class Solution {
public:
string reverseStr(string s, int k) {
int n = s.size();
for(int i = 0; i < n; i += 2*k)
reverse(s.begin() + i, s.begin() + min(i + k, n));
return s;
}
};
``````
Copy The Code &

Input

cmd
s = "abcdefg", k = 2

Output

cmd
"bacdfeg"

### #2 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
public String reverseStr(String s, int k) {
boolean reverse = true;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i += k) {
String substring = s.substring(i, Math.min(i + k, s.length()));
if (reverse) {
sb.append(new StringBuilder().append(substring).reverse().toString());
} else {
sb.append(substring);
}
reverse = !reverse;
}
return sb.toString();
}
}
``````
Copy The Code &

Input

cmd
s = "abcdefg", k = 2

Output

cmd
"bacdfeg"

### #3 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const reverseStr = function(s, k) {
const arr = s.split('')
for(let i = 0, len = s.length; i < len; i += 2 * k) {
helper(arr, i, k)
}
return arr.join('')
};

function helper(arr, start, k) {
let s = start
let e = arr.length > start + k - 1 ? start + k - 1 : arr.length
while(s < e) {
swap(arr, s, e)
s++
e--
}
}

function swap(arr, s, e) {
const tmp = arr[s]
arr[s] = arr[e]
arr[e] = tmp
}
``````
Copy The Code &

Input

cmd
s = "abcd", k = 2

Output

cmd
"bacd"

### #4 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def reverseStr(self, s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
return "".join([s[i:i+k][::-1]+s[i+k:i+2*k]   if len(s)>=i or len(s)>i-k else s[k*i:][::-1] for i in range(0,len(s),k*2)])
``````
Copy The Code &

Input

cmd
s = "abcd", k = 2

Output

cmd
"bacd"

### #5 Code Example with C# Programming

```Code - C# Programming```

``````
using System;
using System.Text;

namespace LeetCode
{
public class _0541_ReverseStringII
{
public string ReverseStr(string s, int k)
{
if (string.IsNullOrEmpty(s) || s.Length == 1) return s;

var sb = new StringBuilder(s);
for (int i = 0; i < s.Length; i += 2 * k)
{
int start = i, end = Math.Min(i + k, s.Length) - 1;
while (start < end)
{
var temp = s[start];
sb[start++] = sb[end];
sb[end--] = temp;
}
}

return sb.ToString();
}
}
}
``````
Copy The Code &

Input

cmd
s = "abcdefg", k = 2

Output

cmd
"bacdfeg"