[프로그래머스] Lv2. 압축 (2018 카카오 공채)

lemythe423·2023년 8월 14일
0
post-thumbnail

🔗

풀이

단순 구현 문제.

우선 딕셔너리에 한 글자 알파벳에 대한 정보를 모두 저장한 후 탐색한다. 딕셔너리에 일치하는 문자가 있다면 탐색을 이어나가고, 일치하는 문자가 없다면 그 문자에 대한 인덱스 값을 저장한다. 이때 result에 추가되는 인덱스 값은 직전에 일치하는 문자가 딕셔너리에 있던 때의 그 값이다.

def solution(msg):
    result = []
    dic = {chr(64+i): i for i in range(1, 27)}
    
    idx, s = 27, ''
    msg = list(msg[::-1])
    while True:
        if s and not dic.get(s):
            result.append(dic[s[:-1]])
            dic[s], s = idx, s[-1]
            idx += 1
            continue
        if not msg:
            result.append(dic[s])
            return result
        s += msg.pop()
profile
아무말이나하기

0개의 댓글