KEEP GOING
[python] 프로그래머스 60057번 : 문자열 압축 (slicing) 본문
반응형
https://programmers.co.kr/learn/courses/30/lessons/60057
1. 정답인 코드
def solution(s):
answer = 1001
n = len(s)
# 테스트케이스 5번 : 문자열 s의 길이가 1일때 처리
if n == 1:
return 1
for unit in range(1, n//2+1):
# print('unit=', unit)
compress = ""
pre = s[0:unit]
# print('pre',pre)
count = 1
# 단위(unit)만큼 크기 증가시켜 비교
for i in range(unit, n, unit):
if pre == s[i:i+unit]:
# print('yes')
count += 1
else:
# 중복 카운팅이 2이상인 경우에만 숫자 포함
compress += str(count) + pre if count >= 2 else pre
pre = s[i:i+unit]
count = 1
# print('compress', compress, end = ' ')
# print('pre',pre)
# else 문에 걸리지 않아 compress에 포함되지 않은 문자열 처리
compress += str(count) + pre if count >= 2 else pre
# print('compress', compress)
answer = min(answer, len(compress))
return answer
반응형
'code review > implementation' 카테고리의 다른 글
[python] 백준 19237번: 어른 상어 (0) | 2022.01.25 |
---|---|
[python] 백준 15686번 : 치킨배달 (combinations, 선형탐색) (0) | 2022.01.11 |
[python] 음수 피보나치 수열 (구현) (0) | 2021.12.03 |
백준 2839번: A -> B (구현) (0) | 2021.11.21 |
백준 4396번 : 지뢰 찾기 (0) | 2021.11.08 |
Comments