[leetcode, JS] 389. Find the Difference

mxxn·2023년 8월 22일
0

leetcode

목록 보기
41/198

문제

문제 링크 : Find the Difference

풀이

/**
 * @param {string} s
 * @param {string} t
 * @return {character}
 */
var findTheDifference = function(s, t) {
    let sArr = new Array(26).fill(0)
    let tArr = new Array(26).fill(0)
    s.split('').forEach( e => {
        sArr[e.charCodeAt(0) - 97] += 1
    })

    t.split('').forEach( e => {
        tArr[e.charCodeAt(0) - 97] += 1
    })
    let result = ''
    sArr.forEach( (e, i) => {
        if( e !== tArr[i]) result = String.fromCharCode([i+97])
    })
    return result
};
  1. 알파벳 개수만큼 array를 2개 만들고
  2. 문자열 s와 t에 각각 알파벳들이 몇개씩 있는지 카운팅
  3. array 2개를 비교하여 카운팅이 다른 값 찾아서 return
  • Runtime 51 ms, Memory 43.4 MB
profile
내일도 글쓰기

0개의 댓글