Algorithm/programmers

문자열 압축 ( Level 2, Python, 2020 Kakao Blind Recruitment )

takeU 2022. 9. 7. 14:29
반응형
def solution(s):
    ans = 10000
    for i in range(1, len(s) // 2 + 1):
        temp = ''
        li = [s[j:j + i] for j in range(0, len(s), i)]
        prev, count, temp = li[0], 1, ''
        for l in li[1:]:
            if prev == l:
                count += 1
            else:
                temp += str(count) + prev if count > 1 else prev
                count = 1
                prev = l
        temp += str(count) + prev if count > 1 else prev
        ans = min(ans, len(temp))
    return ans if len(s) != 1 else 1

문자열