Skip to main content

242. Valid Anagram

分析


题解

function isAnagram(s: string, t: string): boolean {
if (s.length !== t.length) {
return false
}

return s.split("").sort().join("") === t.split("").sort().join("")
};
function isAnagram(s: string, t: string): boolean {
if (s.length !== t.length) { return false }
const memoS: Record<string, number> = {}, memoT: Record<string, number> = {}
for (let i = 0; i < s.length; i++) {
memoS[s[i]] = (memoS[s[i]] === undefined ? 0 : memoS[s[i]]) + 1
memoT[t[i]] = (memoT[t[i]] === undefined ? 0 : memoT[t[i]]) + 1
}
for (const key in memoS) {
if (memoS[key] !== memoT[key]) {
return false
}
}

return true
};