JavaScript Program to Check Anagram
function isAnagram(str1, str2) { return str1.toLowerCase().split('').sort().join('') === str2.toLowerCase().split('').sort().join(''); } which compares sorted lowercase characters of both strings.Examples
How to Think About It
Algorithm
Code
function isAnagram(str1, str2) { return str1.toLowerCase().split('').sort().join('') === str2.toLowerCase().split('').sort().join(''); } console.log(isAnagram('listen', 'silent')); // true console.log(isAnagram('hello', 'world')); // false console.log(isAnagram('Triangle', 'Integral')); // true
Dry Run
Let's trace the example isAnagram('listen', 'silent') through the code
Convert to lowercase
'listen' -> 'listen', 'silent' -> 'silent'
Split into arrays
'listen' -> ['l','i','s','t','e','n'], 'silent' -> ['s','i','l','e','n','t']
Sort arrays
['l','i','s','t','e','n'] -> ['e','i','l','n','s','t'], ['s','i','l','e','n','t'] -> ['e','i','l','n','s','t']
Join arrays
Both become 'eilnst'
Compare strings
'eilnst' === 'eilnst' -> true
| Step | listen | silent | Sorted listen | Sorted silent | Comparison |
|---|---|---|---|---|---|
| 1 | listen | silent | |||
| 2 | ['l','i','s','t','e','n'] | ['s','i','l','e','n','t'] | |||
| 3 | ['e','i','l','n','s','t'] | ['e','i','l','n','s','t'] | |||
| 4 | 'eilnst' | 'eilnst' | |||
| 5 | true |
Why This Works
Step 1: Normalize case
Using toLowerCase() makes sure the comparison ignores uppercase or lowercase differences.
Step 2: Sort characters
Splitting and sorting the characters puts letters in order so we can easily compare the two words.
Step 3: Compare sorted strings
If the sorted strings are exactly the same, it means both words have the same letters in the same amounts, so they are anagrams.
Alternative Approaches
function isAnagram(str1, str2) { if (str1.length !== str2.length) return false; const count = {}; for (const char of str1.toLowerCase()) { count[char] = (count[char] || 0) + 1; } for (const char of str2.toLowerCase()) { if (!count[char]) return false; count[char]--; } return true; } console.log(isAnagram('listen', 'silent')); // true
function isAnagram(str1, str2) { if (str1.length !== str2.length) return false; const map = new Map(); for (const char of str1.toLowerCase()) { map.set(char, (map.get(char) || 0) + 1); } for (const char of str2.toLowerCase()) { if (!map.has(char)) return false; map.set(char, map.get(char) - 1); if (map.get(char) < 0) return false; } return true; } console.log(isAnagram('Triangle', 'Integral')); // true
Complexity: O(n log n) time, O(n) space
Time Complexity
Sorting the characters takes O(n log n) time where n is the length of the string, which dominates the runtime.
Space Complexity
Extra space is used to store the arrays of characters during splitting and sorting, which is O(n).
Which Approach is Fastest?
Counting characters with a frequency map can be faster (O(n)) than sorting, but sorting is simpler and good for short strings.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Sorting and comparing | O(n log n) | O(n) | Simple and short strings |
| Frequency count with object | O(n) | O(n) | Long strings, better performance |
| Frequency count with Map | O(n) | O(n) | Clear key-value handling |