본문 바로가기

알고리즘 공부

[2020 KAKAO BLIND RECRUITMENT] 문자열 압축 | Python3

압축 길이 단위를 1부터 문자열 s의 절반 길이까지 반복문으로 반복되는 문자열 개수를 계산하였다.

 

1
2
3
4
5
6
7
8
9
10
11
12
def solution(s):
    answer = len(s)
    for unit in range(1len(s)//2+1):
        count=1; word=s[:unit]; res='' 
        for i in range(unit, len(s)+1, unit):
            if word==s[i:i+unit]: count+=1
            else
                res+=str(count)+word if count>1 else word
                count=1; word=s[i:i+unit]
        res+=s[i:]
        answer=min(answer, len(res)) 
    return answer
cs