58-2 Left rotate string-python

Topic: The left rotation operation of a string is to transfer several characters in front of the string to the end of the string. Please implement the left rotation operation.

def reverse(arry,begin,end):
    while begin<end:
        arry[begin],arry[end] = arry[end],arry[begin]
        begin +=1
        end -=1

def left_rotate_string(s,n):
    s = list(s)
    ls = len(s)
    reverse(s,0,ls-1)
    reverse(s,0,ls-n-1)
    reverse(s,ls-n,ls-1)
    return ''.join(s)

  Note: The main idea is to use 3 rotations, the first time the string is fully rotated; the second half of the first rotation, with the specified n as the demarcation point, at this time the demarcation point after one rotation becomes len(s)-n; Partial rotation after the third time.

Guess you like

Origin blog.csdn.net/wh672843916/article/details/105503763