给定两个字符串 s 和 t,编写一个函数来判断 t 是否是 s 的字母异位词。
解析
1 2 3 4 5 6 7 8 9 10
| var isAnagram = function (s, t) { if (s.length !== t.length) return false; const count = new Array(26).fill(0); const base = 'a'.charCodeAt(0); for (let i = 0; i < s.length; i++) { count[s.charCodeAt(i) - base]++; count[t.charCodeAt(i) - base]--; } return count.every(c => c === 0); };
|