LeetCode Python/Java/C++/JS  >  String  >  541. Reverse String II  >  Solved in Python, Java, JavaScript, C#, Ruby, C++, Go  >  GitHub or Repost

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

Intuition

Webmaster (Zhang Jian): πŸ‘‹

Hi everyone! I am Zhang Jian.
I know the challenge of transitioning from mastering algorithms to actually landing a great job. That's why, in addition to this resource, I personally developed leader.me!

πŸš€ leader.me is the ultimate all-in-one platform for programmers to build their personal brand, featuring portfolio hosting, resume builders, and integrated blogs.

Build your programmer brand at leader.me β†’


  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.

Step-by-Step Solution

  1. Use a new string result as the return value. In the loop, the step value is k.

    result = ''
    index = 0
    
    while index < s.size
      k_chars = s[index...index + k]
      result += k_chars
      index += k
    end
    
    return result
    
  2. Use the Boolean variable should_reverse as the judgment condition for whether to reverse, and only reverse the first k characters of each 2k 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)
}

Other languages

Welcome to contribute code to LeetCode.blog GitHub -> 541. Reverse String II. Thanks!

Level Up Your Developer Identity

πŸš€ While mastering algorithms is key, showcasing your talent is what gets you hired.
We recommend leader.me β€” the ultimate all-in-one personal branding platform for programmers.

The All-In-One Career Powerhouse:

  • πŸ“„ Resume, Portfolio & Blog: Integrate your skills, projects, and writing into one stunning site.
  • 🌐 Free Custom Domain: Bind your own personal domain for freeβ€”forever.
  • ✨ Premium Subdomains: Stand out with elite tech handle like name.leader.me.

Build Your Programmer Brand at leader.me β†’