[LeetCode] 1880. Check if Word Equals Summation of Two Words

Chobby·3일 전
1

LeetCode

목록 보기
623/650

😎풀이

  1. a ~ z 까지의 알파벳과 그에 상응하는 인덱스 저장
  2. 입력받은 문자열을 숫자 형태로 변환하는 헬퍼 함수 정의
  3. firstWord, secondWord, targetWord를 모두 숫자 형으로 변환
  4. firstWord + secondWord === targetWord인지 검사
const map = new Map()
for(let alphabet = 97, i = 0; i < 26; i++, alphabet++) {
    const curAlphabet = String.fromCharCode(alphabet)
    map.set(curAlphabet, i)
}

function isSumEqual(firstWord: string, secondWord: string, targetWord: string): boolean {
    const firstNum = transStrToNum(firstWord)
    const secondNum = transStrToNum(secondWord)
    const targetNum = transStrToNum(targetWord)
    return (firstNum + secondNum) === targetNum
};

function transStrToNum(str: string) {
    let transfered = ''
    for(const char of str) transfered += map.get(char)
    return Number(transfered)
}
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글