- Notifications
You must be signed in to change notification settings - Fork 846
/
Copy path3.py
22 lines (22 loc) · 1.03 KB
/
3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
defsolution(s):
answer=len(s)
# 1개 단위(step)부터 압축 단위를 늘려가며 확인
forstepinrange(1, len(s) //2+1):
compressed=""
prev=s[0:step] # 앞에서부터 step만큼의 문자열 추출
count=1
# 단위(step) 크기만큼 증가시키며 이전 문자열과 비교
forjinrange(step, len(s), step):
# 이전 상태와 동일하다면 압축 횟수(count) 증가
ifprev==s[j:j+step]:
count+=1
# 다른 문자열이 나왔다면(더 이상 압축하지 못하는 경우라면)
else:
compressed+=str(count) +previfcount>=2elseprev
prev=s[j:j+step] # 다시 상태 초기화
count=1
# 남아있는 문자열에 대해서 처리
compressed+=str(count) +previfcount>=2elseprev
# 만들어지는 압축 문자열이 가장 짧은 것이 정답
answer=min(answer, len(compressed))
returnanswer