simple python solution, straightforward


  • 0
    R
    
    class Solution(object):
        def reverseStr(self, s, k):
            """
            :type s: str
            :type k: int
            :rtype: str
            """
            s = list(s)
            for left in range(0, len(s), 2*k):
                right = min(left + k - 1, len(s)-1)  # right=left+k-1 except right exceed len(s)
                while left < right:
                    s[left], s[right] = s[right], s[left]
                    left += 1
                    right -= 1 
            return ''.join(s)
    

Log in to reply
 

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