[LeetCode] 2451. Odd String Difference

Chobby·2025년 10월 30일
1

LeetCode

목록 보기
737/760

😎풀이

  1. words 순회
    1-1. 각 문자의 사전 순 간격 기록
    1-2. 간격 배열을 문자열 형태로 형 변환 하여 key를 갖는 상태로 현재 word 등록
  2. 기록된 Map 객체 조회
    2-1. 단 하나의 word를 갖는 문자 간격 탐색하여 반환
function oddString(words: string[]): string {
    const map = new Map<string, string[]>()
    for(const word of words) {
        const diff = []
        for(let i = 1; i < word.length; i++) {
            const prev = word[i - 1].charCodeAt(0)
            const cur = word[i].charCodeAt(0)
            diff.push(cur - prev)
        }
        const key = JSON.stringify(diff)
        map.set(key, [...(map.get(key) ?? []), word])
    }
    for(const [key, value] of map) {
        if(value.length === 1) {
            return value[0]
        }
    }
    return ""
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글