https://programmers.co.kr/learn/courses/30/lessons/17684
✅ Solution
- 길이가 1인 단어를 딕셔너리 타입으로 모두 초기화한다.
- 인덱스 값을 1씩 더해가며 사전에 없는 문자/문자열을 찾는다.
- 없는 문자열을 찾았으면 해당 값을 사전에 추가해주고, 맨 마지막 문자를 제외한 문자/문자열을 answer에 추가해준다.
- 그리고 시작 인덱스를 끝나는 인덱스로 바꿔준다. -> 끝나는 인덱스 값이 msg와 같아질 때까지 반복!
✅ Code
def solution(msg):
answer = []
dictionary = {chr(x): x - 64 for x in range(65, 91)}
idx_f, idx_e = 0, 0
while True:
idx_e += 1
if idx_e == len(msg):
answer.append(dictionary[msg[idx_f:idx_e]])
break
if msg[idx_f:idx_e + 1] not in dictionary.keys():
dictionary[msg[idx_f:idx_e + 1]] = len(dictionary) + 1
answer.append(dictionary[msg[idx_f:idx_e]])
idx_f = idx_e
return answer
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] Level 2 - 주차 요금 계산 (Python) (0) | 2022.05.24 |
---|---|
[프로그래머스] Level 2 - 양궁대회 (Python) (2) | 2022.05.24 |
[프로그래머스] Level 2 - [3차] 방금그곡 (Python) (0) | 2022.05.18 |
[프로그래머스] Level 2 - [1차] 캐시 (Python) (0) | 2022.05.17 |
[프로그래머스] Level 2 - [1차] 프렌즈4블록 (Python) (0) | 2022.05.17 |