Skip to content

Commit 3751db5

Browse files
committed
[PGS]#17684. [3차]_압축/lv2/1h40min
https://programmers.co.kr/learn/courses/30/lessons/17684
1 parent b127f01 commit 3751db5

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Hongjoo/lv2/3차_압축.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
## 프로그래머스#17684. [3차]_압축: 구현 / lv2
3+
> 문제 링크 : https://school.programmers.co.kr/learn/courses/30/lessons/17684
4+
"""
5+
def solution(msg):
6+
answer = []
7+
# 1. 사전 초기기
8+
dict = {}
9+
idx = 1 # 초기 사전 idx
10+
for o in range(65, 65+ 26) :
11+
dict[chr(o)] = idx
12+
idx += 1
13+
last_idx = idx-1
14+
#2. 입출력 단어 압축하기
15+
prev_idx = -1
16+
w_s = 0 ; w_l = 1
17+
while w_s+w_l <= len(msg) :
18+
word = msg[w_s : w_s + w_l]
19+
idx = dict.get(word , -1 )
20+
if idx > -1 :
21+
prev_idx = idx
22+
w_l += 1
23+
continue
24+
#사전에 없음
25+
else :
26+
last_idx += 1
27+
dict[word] = last_idx # 사전 등록하기
28+
w_s = w_s + w_l - 1 #탐색 point 업데이트
29+
w_l = 1 # 단어 길이 초기화
30+
answer+= [prev_idx]# 이전 존재한 단어의 idx 는 출력 리스트에 추가
31+
answer+= [prev_idx] # 마지막 w 의 인덱스 출력
32+
33+
34+
return answer

0 commit comments

Comments
 (0)