Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Programmers/압축/wooyeol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
압축
https://school.programmers.co.kr/learn/courses/30/lessons/17684

풀이시간 22:30 ~ 23:00 | 23:53 ~ 00:27 (1시간 4분)

접근법
무슨 알고리즘으로 풀이 할 수 있을까? -> 구현

1. W(현재 입력) 값을 찾기
- 가장 긴 사전에 있는 문자열 w를 찾기
2. 가장 긴 W의 사전 인덱스 값을 answer에 삽입
3. W+C를 사전에 등록하기

"""
from typing import List


def solution(msg: str):
answer: List = []

msg_len: int = len(msg)
cur: int = 0
last_index: int = 26

# 사전 구현
bag = {chr(65 + code): code + 1 for code in range(26)}

while cur < msg_len:
# 1. w(현재 입력)가정 값을 지정
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제에 주어진 조건대로 구현하시고 주석을 달아주셔서 직관적으로 이해가 되는 코드인것 같아요! 👍👍👍

w = msg[cur]

# 1. 가장 긴 문자열 w를 찾기
while cur + 1 < msg_len and w + msg[cur + 1] in bag:
w += msg[cur + 1]
cur += 1
# print("2 : ", w)

# 2. 가장 긴 W의 사전 인덱스 값을 answer에 삽입
answer.append(bag[w])

# 3. 사전에 w+c를 등록
if cur + 1 < msg_len and w + msg[cur + 1] not in bag:
last_index += 1
bag[w + msg[cur + 1]] = last_index

cur += 1

return answer


case1 = "KAKAO"
case2 = "TOBEORNOTTOBEORTOBEORNOT"
case3 = "ABABABABABABABAB"

print(solution(case1))
print(solution(case2))
print(solution(case3))