Problems › TypeScript › warmup
Are these two an anagram
A word game checks whether one phrase uses exactly the same letters as another.
- Case does not matter.
- Whitespace is ignored entirely.
- Everything else — digits, punctuation — counts as a character that has to match.
- Two empty phrases are an anagram of each other.
isAnagram(text: string, other: string) → bool
Where you start
function isAnagram(text: string, other: string): boolean {
}
Worked examples
| Call | Result |
|---|---|
isAnagram("Listen", "Silent") | true |
isAnagram("a b", "ba") | true |
isAnagram("hello", "world") | false |
isAnagram("abc", "ab") | false |
Hint
Strip and lowercase both, then compare the sorted characters, or count them.
Reference solution in TypeScript
function isAnagram(text: string, other: string): boolean {
const clean = (s: string) => s.toLowerCase().replace(/\s+/g, '').split('').sort().join('');
return clean(text) === clean(other);
}