Arena / Problems /

541. Reverse String II

Easy 0/10 Thrones Claimed
Prev Next

Problem Specification

LeetCode #541

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 <= 10000
  • s consists of only lowercase English letters.
  • 1 <= k <= 10000
Topics & Algorithms
⚔️

Vacant Python Throne

No 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.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Python Solution by Anonymous Developer
0
Approach Intuition:
  1. The question does not require reverse in place, so using a new string result as the return value is easier to operate.
  2. In the loop, it is more convenient to use k as the step value rather than 2k, because if 2k is used, k must still be used for judgment.
  3. It is required to reverse only the first 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
⚔️

Vacant Java Throne

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.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Java Solution by Anonymous Developer
0
Approach Intuition:
  1. The question does not require reverse in place, so using a new string result as the return value is easier to operate.
  2. In the loop, it is more convenient to use k as the step value rather than 2k, because if 2k is used, k must still be used for judgment.
  3. It is required to reverse only the first 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();
    }
}
⚔️

Vacant Javascript Throne

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.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Javascript Solution by Anonymous Developer
0
Approach Intuition:
  1. The question does not require reverse in place, so using a new string result as the return value is easier to operate.
  2. In the loop, it is more convenient to use k as the step value rather than 2k, because if 2k is used, k must still be used for judgment.
  3. It is required to reverse only the first 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
};
⚔️

Vacant C# Throne

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.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative C# Solution by Anonymous Developer
0
Approach Intuition:
  1. The question does not require reverse in place, so using a new string result as the return value is easier to operate.
  2. In the loop, it is more convenient to use k as the step value rather than 2k, because if 2k is used, k must still be used for judgment.
  3. It is required to reverse only the first 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;
    }
}
⚔️

Vacant Ruby Throne

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.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Ruby Solution by Anonymous Developer
0
Approach Intuition:
  1. The question does not require reverse in place, so using a new string result as the return value is easier to operate.
  2. In the loop, it is more convenient to use k as the step value rather than 2k, because if 2k is used, k must still be used for judgment.
  3. It is required to reverse only the first 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
⚔️

Vacant C++ Throne

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.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative C++ Solution by Anonymous Developer
0
Approach Intuition:
  1. The question does not require reverse in place, so using a new string result as the return value is easier to operate.
  2. In the loop, it is more convenient to use k as the step value rather than 2k, because if 2k is used, k must still be used for judgment.
  3. It is required to reverse only the first 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;
    }
};
⚔️

Vacant Go Throne

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.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Go Solution by Anonymous Developer
0
Approach Intuition:
  1. The question does not require reverse in place, so using a new string result as the return value is easier to operate.
  2. In the loop, it is more convenient to use k as the step value rather than 2k, because if 2k is used, k must still be used for judgment.
  3. It is required to reverse only the first 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)
}
⚔️

Vacant Rust Throne

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.

⚔️

Vacant Swift Throne

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.

⚔️

Vacant Kotlin Throne

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.