This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
[2023-08-28] wooyeol #144 #167
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(현재 입력)가정 값을 지정 | ||
| 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)) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
문제에 주어진 조건대로 구현하시고 주석을 달아주셔서 직관적으로 이해가 되는 코드인것 같아요! 👍👍👍