Examples
Write isAnagram so that the following cases hold.
- Input
isAnagram("listen", "silent")Outputtrue - Input
isAnagram("Dormitory", "Dirty Room")Outputtrue - Input
isAnagram("hello", "world")Outputfalse
1 more hidden test cases - 4 in total will be graded.
Answer
js
function isAnagram(a, b) {
const norm = (s) =>
s.toLowerCase().replace(/\s/g, "").split("").sort().join("");
return norm(a) === norm(b);
}
isAnagram("Dormitory", "Dirty Room"); // true
Normalise-then-sort is O(n log n) and readable. For O(n), count character frequencies into an object and compare those.
