def solution(s):
answer = len(s) # 최소 길이로 초기화
# 1부터 문자열의 절반까지 단위 길이로 압축을 시도
for step in range(1, len(s) // 2 + 1):
compressed = ""
prev = s[0:step] # 이전 문자열
count = 1 # 반복 횟수
# step부터 문자열의 끝까지 step 단위로 확인
for j in range(step, len(s), step):
# 이전 문자열과 동일한 경우
if prev == s[j:j + step]:
count += 1
else:
# 이전 문자열이 반복된 횟수와 함께 compressed에 추가
compressed += str(count) + prev if count >= 2 else prev
prev = s[j:j + step] # 다음 문자열로 업데이트
count = 1 # 초기화
# 남은 문자열에 대해서 처리
compressed += str(count) + prev if count >= 2 else prev
# 압축된 문자열의 길이와 answer를 비교하여 더 작은 값으로 갱신
answer = min(answer, len(compressed))
return answer
s = "aabbaccc"
>> 7
1. 변수명 변경
2. 압축 단위별로 반복
3. 압축 로직
4. 문자열 검사
5. 문자열 처리
6. 남은 문자열 처리
7. 최소 길이 갱신
8. 결과 반환
