Given two strings s and t , write a function to determine if t is an anagram of s.
var isAnagram = function(s, t) {
if ( s.length === 0 || t.length === 0){
return true;
}
if ( s.length !== t.length ) {
return false;
}
let sObj = {};
let tObj = {};
for(let i = 0; i < s.length; i++ ){
if ( !(s[i] in sObj) ){
sObj[s[i]] = 1;
} else {
sObj[s[i]]++;
}
}
for(let j = 0; j < t.length; j++ ){
if ( !(t[j] in tObj) ){
tObj[t[j]] = 1;
} else {
tObj[t[j]]++;
}
}
for(let key in sObj){
if (!(key in tObj)) {
return false;
}
if (sObj[key] !== tObj[key]){
return false;
}
}
return true;
}
...하...ㅠㅠ... 배열을 왜 생각안했을까..
var isAnagram = function(s, t) {
return s.split('').sort().join('') === t.split('').sort().join('');
};