코드
def solution(s):
answer = 1e10
tmp=''
if len(s)==1:
return 1
for i in range(1, len(s)):
tmp+=s[0:i]
cnt=1
res=''
for j in range(i,len(s),i):
if s[j:j+i]==tmp:
cnt+=1
else:
if cnt>1:
res+=str(cnt)+tmp
tmp=s[j:j+i]
cnt=1
else:
res+=tmp
tmp=s[j:j+i]
if cnt>1:
res+=str(cnt)+tmp
else:
res+=tmp
tmp=''
answer=min(answer,len(res))
return answer
다른 사람의 풀이
def compress(text, tok_len):
words = [text[i:i+tok_len] for i in range(0, len(text), tok_len)]
res = []
cur_word = words[0]
cur_cnt = 1
for a, b in zip(words, words[1:] + ['']):
if a == b:
cur_cnt += 1
else:
res.append([cur_word, cur_cnt])
cur_word = b
cur_cnt = 1
return sum(len(word) + (len(str(cnt)) if cnt > 1 else 0) for word, cnt in res)
def solution(text):
return min(compress(text, tok_len) for tok_len in list(range(1, int(len(text)/2) + 1)) + [len(text)])