代码:
var isAnagram = function(s, t) {
// return s.split('').sort().join('') === t.split('').sort().join('')
let getObj = function(str) {
let res = {}
for (let i = 0; i < str.length; i++) {
let v = str.charCodeAt(i);
if (res[v]) {
res[v]++;
} else {
res[v] = 1;
}
}
return res;
}
let sObj = getObj(s);
let tObj = getObj(t);
let sKeys = Object.keys(sObj);
let tKeys = Object.keys(tObj);
if (sKeys.length !== tKeys.length) {
return false;
} else {
for (let i = 0; i < sKeys.length; i++) {
let v = sKeys[i];
if (!tObj[v] || tObj[v] !== sObj[v]) {
return false;
}
}
return true;
}
};