티스토리 뷰

320x100

2021년 2월 24일 Velog에 작성할 글을 옮겨온 글입니다.

프로그래머스 - [3차] 압축
function solution(msg) {
    let answer = [];
    let dictionary = [''];
    for (let i = 65; i < 91; i++) { // 1 사전 초기화
        dictionary.push(String.fromCharCode(i));
    }
    for (let i = 0, j, len = msg.length; i < len; i = j) {
        let w = msg[i];
        for (j = i + 1; j < len; j++) { // 2 현재 입력 중 사전에 등재되어 있는 가장 긴 문자열 w 찾기
            let c = msg[j];
            if (!dictionary.includes(w + c)) {
                dictionary.push(w + c); // 4 w + c 사전에 등록
                break;
            }
            w += msg[j];
        }
        answer.push(dictionary.indexOf(w)); // 3 w의 사전 색인 번호 출력
    }
    return answer;
}

320x100
댓글