easy to understand in python


  • 0
    Z
    class Solution(object):
        def reverseStr(self, s, k):
            """
            :type s: str
            :type k: int
            :rtype: str
            """
            res=''
            while s:
                if len(s)<k:
                    res+=s[::-1]
                    return res
                elif k<=len(s)<=2*k:
                    res+= s[:k][::-1]+s[k:]
                    return res
                else:
                    res+=s[0:k][::-1]+s[k:2*k]
                    s=s[2*k:]
            return
    

Log in to reply
 

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