[LeetCode] 2325. Decode the Message

Chobby·2025년 10월 26일
1

LeetCode

목록 보기
712/746

😎풀이

  1. key를 순회
    1-1. 공백일 경우 생략
    1-2. 탐색 순서대로 인덱스 부여
  2. message 순회
    2-1. 공백일 경우 공백으로 치환
    2-2. key에 해당하는 알파벳 순서에 맞게 치환
    2-3. 복호화 문자열에 추가
  3. 복호화 문자열 반환
function decodeMessage(key: string, message: string): string {
    const map = new Map()
    for(const char of key) {
        if(map.has(char)) continue
        if(char === ' ') continue
        map.set(char, map.size)
    }
    let decoded = ''
    for(const char of message) {
        if(char === ' ') {
            decoded += ' '
            continue
        }
        const substitution = map.get(char) ?? 0
        const charCode = substitution + 97
        decoded += String.fromCharCode(charCode)
    }
    return decoded
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글