Problem Solving
10. 문자열 압축
굥깡
2022. 8. 31. 00:50
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/60057
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
주어진 문자열에 대해 aabcaabcaabc -> 2bc2bc2bc (길이 9) or 3aabc (길이 5) 등으로 압축하는 문제
압축 가능한 경우의 수 중 가장 짧은 길이를 구하면 됨
def solution(s):
answer = len(s)
for i in range(1, len(s)):
listS = split(s, i)
for j in range(len(listS)-1):
if listS[j] == listS[j+1]:
listS[j] = '2'
if j != 0 and listS[j-1].isdigit():
listS[j] = str(int(listS[j-1])+1)
listS[j-1] = ''
stringS = ''.join(listS)
if len(stringS) < answer:
answer = len(stringS)
return answer
def split(s, numb):
list1 = []
max = numb
for i in range(0, len(s), numb):
if max < len(s):
list1.append(s[i:i+numb])
max = i+numb
return list1
문제는 쉽게 이해할 수 있었으나 로직을 고민하는 게 오래 걸렸고 무엇보다 코드가 깔끔하지 않은 것 같아서 풀면서 상당히 기분이 안 좋았음ㅠㅠㅠㅠ