https://programmers.co.kr/learn/courses/30/lessons/60057?language=python3
코딩테스트 연습 - 문자열 압축
데이터 처리 전문가가 되고 싶은 "어피치"는 문자열을 압축하는 방법에 대해 공부를 하고 있습니다. 최근에 대량의 데이터 처리를 위한 간단한 비손실 압축 방법에 대해 공부를 하고 있는데, 문
programmers.co.kr
def solution(s):
temp = []
for n_cut in range(1,len(s)//2+1):
cnt = 0
compressed = ''
for cut in range(0,len(s),n_cut):
if s[cut:cut+n_cut] == s[cut+n_cut:cut+(n_cut*2)]:
cnt +=1
else:
if cnt != 0:
compressed += str(cnt) + s[cut:cut+n_cut]
cnt = 0
else:
compressed += s[cut:cut+n_cut]
temp.append(len(compressed))
return min(temp)
def solution(s):
#일단 내가 해결하기 힘든부분은 그냥 반복이나 노가다성 코드로 돌리자
length = len(s)
answer = [length]
for size in range(1,length):
compressed = ""
splited = [s[i:i+size] for i in range(0, length, size)]
count = 1
for j in range(1,len(splited)):
if splited[j-1] == splited[j]:
count += 1
else:
compressed += str(count)+splited[j-1] if count>1 else splited[j-1]
count = 1
compressed += str(count)+splited[-1] if count>1 else splited[-1]
answer.append(len(compressed))
return min(answer)
'B > Coding Test' 카테고리의 다른 글
[백준 알고리즘] 집합의 표현 - 유니온 파인드 (0) | 2021.10.08 |
---|---|
[프로그래머스] MySQL - 우유와 요거트가 담긴 장바구니 (0) | 2021.10.08 |
[코딩테스트 연습] - 124 나라의 숫자 (0) | 2021.10.07 |
[프로그래머스] DFS/BFS - 단어 변환 (0) | 2021.10.06 |
[코딩테스트] 완전탐색 - 모의고사 / 카펫 (0) | 2021.10.01 |