Fuck LeetCode > String > 541. Reverse String II > Solved in Python, Java, JavaScript, C#, Ruby, C++, Go > Repost or Contribute
LeetCode link: 541. Reverse String II, difficulty: Easy.
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 tok
characters, then reverse the firstk
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 <= 10000
s
consists of only lowercase English letters.1 <= k <= 10000
Intuition
- The question does not require
reverse in place
, so using a new stringresult
as the return value is easier to operate. - In the loop, it is more convenient to use
k
as the step value rather than2k
, because if2k
is used,k
must still be used for judgment. - It is required to reverse only the first
k
characters of each2k
characters, so aboolean
variableshould_reverse
is needed as a judgment condition for whether to reverse.
Steps
Use a new string
result
as the return value. In the loop, the step value isk
.result = '' index = 0 while index < s.size k_chars = s[index...index + k] result += k_chars index += k end return result
Use the Boolean variable
should_reverse
as the judgment condition for whether to reverse, and only reverse the firstk
characters of each2k
characters.result = '' should_reverse = true # 1 index = 0 while index < s.size k_chars = s[index...index + k] if should_reverse # 2 result += k_chars.reverse # 3 else # 4 result += k_chars end index += k should_reverse = !should_reverse # 5 end return result
Complexity
Time complexity
O(N)
Space complexity
O(N)
Python #
class Solution:
def reverseStr(self, s: str, k: int) -> str:
result = ''
should_reverse = True
index = 0
while index < len(s):
k_chars = s[index:index + k]
if should_reverse:
result += k_chars[::-1]
else:
result += k_chars
index += k
should_reverse = not should_reverse
return result
Java #
class Solution {
public String reverseStr(String s, int k) {
var result = new StringBuffer();
var shouldReverse = true;
var index = 0;
while (index < s.length()) {
var kChars = s.substring(index, Math.min(index + k, s.length()));
if (shouldReverse) {
result.append(new StringBuffer(kChars).reverse());
} else {
result.append(kChars);
}
index += k;
shouldReverse = !shouldReverse;
}
return result.toString();
}
}
JavaScript #
var reverseStr = function (s, k) {
let result = ''
let shouldReverse = true
let index = 0
while (index < s.length) {
const kChars = s.substr(index, k)
if (shouldReverse) {
result += [...kChars].reverse().join('')
} else {
result += kChars
}
index += k
shouldReverse = !shouldReverse
}
return result
};
C# #
public class Solution
{
public string ReverseStr(string s, int k)
{
string result = "";
bool shouldReverse = true;
int index = 0;
while (index < s.Length)
{
string kChars = s[index..Math.Min(index + k, s.Length)];
if (shouldReverse)
{
result += new string(kChars.Reverse().ToArray());
}
else
{
result += kChars;
}
index += k;
shouldReverse = !shouldReverse;
}
return result;
}
}
Ruby #
def reverse_str(s, k)
result = ''
should_reverse = true
index = 0
while index < s.size
k_chars = s[index...index + k]
if should_reverse
result += k_chars.reverse
else
result += k_chars
end
index += k
should_reverse = !should_reverse
end
result
end
C++ #
class Solution {
public:
string reverseStr(string s, int k) {
string result = "";
bool shouldReverse = true;
int index = 0;
while (index < s.length()) {
auto kChars = s.substr(index, k);
if (shouldReverse) {
reverse(kChars.begin(), kChars.end());
}
result += kChars;
index += k;
shouldReverse = !shouldReverse;
}
return result;
}
};
Go #
func reverseStr(s string, k int) string {
var result []rune
shouldReverse := true
index := 0
for index < len(s) {
end := index + k
if end > len(s) {
end = len(s)
}
kChars := []rune(s[index:end])
if shouldReverse {
for i, j := 0, len(kChars) - 1; i < j; i, j = i + 1, j - 1 {
kChars[i], kChars[j] = kChars[j], kChars[i]
}
}
result = append(result, kChars...)
index += k
shouldReverse = !shouldReverse
}
return string(result)
}