力扣题目:给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
示例 1: 输入: s = "anagram", t = "nagaram" 输出: true
示例 2: 输入: s = "rat", t = "car" 输出: false
说明: 你可以假设字符串只包含小写字母
异位词指的是长度和包含字母种类一致,只是换了字母位置。
var isAnagram = function (oldNum, newNum) {
if (oldNum.length != newNum.length) {
return false;
}
const a = 'a'.charCodeAt();
const resSet = new Array(26).fill(0);
for (const i of oldNum) {
resSet[i.charCodeAt() - a]++;
}
for (const i of newNum) {
if (!resSet[i.charCodeAt() - a]) {
return false
}
resSet[i.charCodeAt() - a]--;
}
return true;
}
var nums1 = 'apple'
var nums2 = 'eppla'
var a = isAnagram(nums1, nums2);
console.log(a)