We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent b127f01 commit 3751db5Copy full SHA for 3751db5
1 file changed
Hongjoo/lv2/3차_압축.py
@@ -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