Check Anagrams
Write a TypeScript function that checks if two strings are anagrams — they contain the same characters with the same frequencies. Ignore case and non-alphanumeric characters.
Build a character frequency object from the first string. For each character, increment its count. Then loop through the second string and decrement the count for each character. If a character has zero count before decrementing, the strings are not anagrams. After processing both strings, verify all counts are zero.
An alternative approach sorts both strings and compares them directly: s1.split('').sort().join('') === s2.split('').sort().join(''). The frequency counting approach is more efficient at O(n) time compared to O(n log n) for sorting.
Edge cases include empty strings (both empty are anagrams), strings of different lengths (cannot be anagrams), case sensitivity issues, and strings with spaces and punctuation.
TypeScript's type annotations for the frequency object use the `any` type since the object keys are dynamic characters rather than predefined properties. The function returns a boolean indicating anagram status.
The character frequency approach uses an object as a hash map for O(1) lookups. The optional chaining with || operator provides a default value of 0 when a key doesn't exist yet.
Example Input & Output
Algorithm Flow

Solution Approach
Write a TypeScript function that checks if two strings are anagrams — they contain the same characters with the same frequencies. Ignore case and non-alphanumeric characters.
Build a character frequency object from the first string. For each character, increment its count. Then loop through the second string and decrement the count for each character. If a character has zero count before decrementing, the strings are not anagrams. After processing both strings, verify all counts are zero.
An alternative approach sorts both strings and compares them directly: s1.split('').sort().join('') === s2.split('').sort().join(''). The frequency counting approach is more efficient at O(n) time compared to O(n log n) for sorting.
Edge cases include empty strings (both empty are anagrams), strings of different lengths (cannot be anagrams), case sensitivity issues, and strings with spaces and punctuation.
TypeScript's type annotations for the frequency object use the `any` type since the object keys are dynamic characters rather than predefined properties. The function returns a boolean indicating anagram status.
The character frequency approach uses an object as a hash map for O(1) lookups. The optional chaining with || operator provides a default value of 0 when a key doesn't exist yet.
Best Answers
function isAnagram(s1: string, s2: string): boolean {
if (s1.length !== s2.length) return false;
var freq: any = {};
for (var i = 0; i < s1.length; i++) {
freq[s1.charAt(i)] = (freq[s1.charAt(i)] || 0) + 1;
}
for (var i = 0; i < s2.length; i++) {
var ch = s2.charAt(i);
if (!freq[ch]) return false;
freq[ch]--;
}
return true;
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
