728x90
헐... 나는 진짜 어렵다고 생각했는데(효율적인 방법이 있는줄)
그냥( 2 ~ 절반길이 )까지 모두 구하고
그중에서 제일 짧은 문자열 고르면 됨
이중for문
문자열 slice
def solution(s):
answer = len(s)
for step in range(1, len(s)//2 + 1):
compressed = ""
before = s[0:step]
count = 1
for j in range(step, len(s), step):
if before == s[j:j+step]:
count +=1
else: #다른 문자열이 나온거면
if count >= 2:
compressed += str(count) + before
else:
compressed += before
before = s[j:j+step] #다시 상태초기화
count = 1
#남아있는 문자열에 대해 처리
if count >= 2:
compressed += str(count) + before
else:
compressed += before
answer = min(answer, len(compressed))
return answer
728x90