Algorithms and Data Structures/Coding Practices

AlgoExpert Run-Length Encoding

brightlightkim 2022. 7. 19. 09:18

def runLengthEncoding(string):
    c = string[0]
    n = 0
    st = ""
    for ch in string:
        if (c != ch or n > 8):
            st += (str(n) + c)
            c = ch
            n = 1
        else:
            n +=1
    return st + str(n) + c