🚩 문제 설명
https://programmers.co.kr/learn/courses/30/lessons/60057
◾ 주어진 문자열을 최적화하여 압축하는 문제
✅ 입출력
s | return |
"aabbaccc" | 7 |
"ababcdcdababcdcd" | 9 |
"abcabcdede" | 8 |
"abcabcabcabcdededededede" | 14 |
"xababcdcdababcdcd" | 17 |
📑 문제 풀이
with 파이썬 (Python)
def solution(s):
ans = len(s)
for i in range(1, len(s) // 2 + 2):
res = ""
cnt = 1
tmp = s[:i]
for j in range(i, len(s) + i, i):
if tmp == s[j:j + i]:
cnt += 1
else:
if cnt == 1:
res += tmp
else:
res += str(cnt) + tmp
tmp = s[j:j + i]
cnt = 1
ans = min(ans, len(res))
return ans
728x90
'✏️ 𝗔𝗹𝗴𝗼𝗿𝗶𝘁𝗵𝗺 > 프로그래머스' 카테고리의 다른 글
[PG67256] 키패드 누르기 (0) | 2021.11.06 |
---|---|
[PG81301] 숫자 문자열과 영단어 (0) | 2021.11.06 |
[PG72410] 신규 아이디 추천 (0) | 2021.11.06 |
[PG42748] K번째수 (0) | 2021.11.06 |
[PG42862] 체육복 (0) | 2021.10.15 |