simple python solution

  • 0
    class Solution(object):
        def reverseStr(self, s, k):
            :type s: str
            :type k: int
            :rtype: str
            new_str = ''
            segments = len(s) // k
            if not segments:
                return s[::-1]
            for i in range(0, segments*k + 1, 2*k):
                new_str += s[i: i + k][::-1] + s[i + k: i + 2*k]
            return new_str

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.