Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string.
k characters left, reverse all of them.2k but greater than or equal to k characters, then reverse the first k characters and leave the other as original.1 <= s.length <= 10000s consists of only lowercase English letters.1 <= k <= 10000No developer has claimed the Python throne for LeetCode #541 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
reverse in place, so using a new string result as the return value is easier to operate.k as the step value rather than 2k, because if 2k is used, k must still be used for judgment.k characters of each 2k characters, so a boolean variable should_reverse is needed as a judgment condition for whether to reverse.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
No developer has claimed the Java throne for LeetCode #541 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
reverse in place, so using a new string result as the return value is easier to operate.k as the step value rather than 2k, because if 2k is used, k must still be used for judgment.k characters of each 2k characters, so a boolean variable should_reverse is needed as a judgment condition for whether to reverse.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();
}
}
No developer has claimed the Javascript throne for LeetCode #541 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
reverse in place, so using a new string result as the return value is easier to operate.k as the step value rather than 2k, because if 2k is used, k must still be used for judgment.k characters of each 2k characters, so a boolean variable should_reverse is needed as a judgment condition for whether to reverse.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
};
No developer has claimed the C# throne for LeetCode #541 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
reverse in place, so using a new string result as the return value is easier to operate.k as the step value rather than 2k, because if 2k is used, k must still be used for judgment.k characters of each 2k characters, so a boolean variable should_reverse is needed as a judgment condition for whether to reverse.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;
}
}
No developer has claimed the Ruby throne for LeetCode #541 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
reverse in place, so using a new string result as the return value is easier to operate.k as the step value rather than 2k, because if 2k is used, k must still be used for judgment.k characters of each 2k characters, so a boolean variable should_reverse is needed as a judgment condition for whether to reverse.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
No developer has claimed the C++ throne for LeetCode #541 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
reverse in place, so using a new string result as the return value is easier to operate.k as the step value rather than 2k, because if 2k is used, k must still be used for judgment.k characters of each 2k characters, so a boolean variable should_reverse is needed as a judgment condition for whether to reverse.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;
}
};
No developer has claimed the Go throne for LeetCode #541 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
reverse in place, so using a new string result as the return value is easier to operate.k as the step value rather than 2k, because if 2k is used, k must still be used for judgment.k characters of each 2k characters, so a boolean variable should_reverse is needed as a judgment condition for whether to reverse.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)
}
No developer has claimed the Rust throne for LeetCode #541 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #541 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #541 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.