39/60 testcases stuck.. python...please need your help.. thanks


  • 0
    A
    class Solution(object):
        def reverseStr(self, s, k):
            """
            :type s: str
            :type k: int
            :rtype: str
            """
            
            iter=(len(s)//(2*k)) //minimum passes over string in for loop
            if(iter==0):         // in case iter=0, execute loop once
                iter=1
            revs=''              //store reversed string
            for i in range(0,iter,2*k):
                revs+=s[i:i+k][::-1]
                revs+=s[i+k:i+2*k]
            ind=len(revs)-1      //index of character from where we begin to start applying next conditions
            if(ind==0):
                return revs
            if(len(s[ind+1:])) < k:
                revs+=s[ind+1:][::-1]
            else:
                revs+=s[ind+1:ind+k+1][::-1]
                revs+=s[ind+k+1:]
            return revs  
    

    Input "krmyfshbspcgtesxnnljhfursyissjnsocgdhgfxubewllxzqhpasguvlrxtkgatzfybprfmmfithphckksnvjkcvnsqgsgosfxc"
    20

    Expected output:
    "jlnnxsetgcpsbhsfymrkhfursyissjnsocgdhgfxtxrlvugsaphqzxllwebukgatzfybprfmmfithphccxfsogsgqsnvckjvnskk"

    Output that I'm getting:
    "jlnnxsetgcpsbhsfymrkhfursyissjnsocgdhgfxtxrlvugsaphqzxllwebukgatzfybprfmmfithphckksnvjkcvnsqgsgosfxc"

    Please suggest where I can improve the code. Thanks


Log in to reply
 

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