https://leetcode.com/problems/valid-anagram/description/
var isAnagram = function(s, t) {
if(s.length !== t.length) return false;
const sMap = {};
const tMap = {};
[...s].forEach(key => sMap[key] = (sMap[key] || 0) + 1 );
[...t].forEach(key => tMap[key] = (tMap[key] || 0) + 1 );
for(let sKey of Object.keys(sMap)) {
if(!tMap[sKey]) {
return false;
}
if(tMap[sKey] !== sMap[sKey]) {
return false;
}
}
return true;
};
객체에 키 값이 있는지 여부를 판단하고 초기화 하는 코드가 마음에 듬
'Algorithm & 자료구조' 카테고리의 다른 글
[알고리즘 JS] 뉴스 클러스터링(프로그래머스 Lv.2) (0) | 2023.01.31 |
---|