[알고리즘] 3차 압축

nerry·2024년 1월 28일
0

알고리즘

목록 보기
86/86

전체 코드

def solution(msg):
    answer = []
    dict = {}
    for i in range(65,65+26):
        dict[chr(i)] = i-65+1
        
    while len(msg)>0:
        if len(msg) == 1:
            answer.append(dict[msg])
            break
        temp = msg[0]
        idx = 0
        while len(temp) < len(msg):
            now = temp
            idx+=1
            temp+=msg[idx]
            if temp not in dict:
                temp = now
                break
        answer.append(dict[temp])
        msg = msg[len(temp):]
        if msg:
            dict[temp+msg[0]] = len(dict)+1
    return answer

해법

  1. 긴 문자열 찾기 : 단순 탐색 방식 사용
  2. 없을 경우 dict에 저장
profile
터벅터벅 개발(은좋은)자 로그

0개의 댓글