Code Logo

Check Anagrams

Published at24 Jul 2026
TypeScript String Handling Medium 0 views
Like0

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

Example 1
Input
"hello","world"
Output
false
Example 2
Input
"listen","silent"
Output
true
Example 3
Input
"",""
Output
true
Example 4
Input
"anagram","nagaram"
Output
true
Example 5
Input
"rat","car"
Output
false

Algorithm Flow

Recommendation Algorithm Flow for Check Anagrams
Recommendation Algorithm Flow for Check Anagrams

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

typescript - Approach 1
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;
}