2020 KAKAO BLIND RECRUITMENT 문자열 압축 (lv2)

Yibangwon·2022년 4월 23일
0

알고리즘 문제풀이

목록 보기
30/60

정답 코드

def compress(text, tok_len):
    words = [text[i:i + tok_len] for i in range(0, len(text), tok_len)]
    res = []
    cnt = 1
    cur_word = words[0]
    for a, b in zip(words, words[1:] + ['']):
        if a == b:
            cnt += 1
        else:
            res.append([cur_word, cnt])
            cur_word = b
            cnt = 1
    return sum(len(a) + (len(str(b)) if b > 1 else 0) for a, b in res)

def solution(s):
    return min(compress(s, token) for token in list(range(1, int(len(s)/2) + 1)) + [len(s)])
    

배운 점

  • 파이썬다운 1줄 코딩이 무엇인지
profile
I Don’t Hope. Just Do.

0개의 댓글