Skip to content

Commit 2755082

Browse files
authored
Create 3.py
1 parent 8b05e17 commit 2755082

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

12/3.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def solution(s):
2+
answer = len(s)
3+
# 1개 단위(step)부터 압축 단위를 늘려가며 확인
4+
for step in range(1, len(s)):
5+
compressed = ""
6+
prev = s[0:step] # 앞에서부터 step만큼의 문자열 추출
7+
count = 1
8+
# 단위(step) 크기만큼 증가시키며 이전 문자열과 비교
9+
for j in range(step, len(s), step):
10+
# 이전 상태와 동일하다면 압축 횟수(count) 증가
11+
if prev == s[j:j + step]:
12+
count += 1
13+
# 다른 문자열이 나왔다면 (더 이상 압축하지 못하는 경우라면)
14+
else:
15+
compressed += str(count) + prev if count >= 2 else prev
16+
prev = s[j:j + step] # 다시 상태 초기화
17+
count = 1
18+
# 남아있는 문자열에 대해서 처리
19+
compressed += str(count) + prev if count >= 2 else prev
20+
# 만들어지는 압축 문자열이 가장 짧은 것이 정답
21+
answer = min(answer, len(compressed))
22+
return answer

0 commit comments

Comments
 (0)