Valid Anagram
Given two strings s and t, return true if t is an anagram of s, and false otherwise. Two words are anagrams when they use exactly the same letters exactly the same number of times, only in a different order. Count every letter of s in a hash map from letter to how many times it appears, then walk through t and take one away for each letter you meet. If a letter is missing, or the two strings are different lengths, they cannot be anagrams.
Constraints
- 1 ≤ s.length, t.length ≤ 5 × 104
- s and t consist of lowercase English letters
- The comparison is case sensitive
Example
s = "anagram", t = "nagaram"trueExplanation Both words contain three a, one n, one g, one r and one m, so t is a rearrangement of s.
In plain terms
- Anagram
- A word made by rearranging all the letters of another word, using every letter the same number of times. "listen" and "silent" are anagrams.
- Hash map
- A lookup table that stores pairs of key and value. Here the key is a letter and the value is how many times that letter appeared. Looking a key up takes roughly the same tiny amount of time no matter how many keys are stored.
- Counter
- A hash map used to hold tallies. Every time you meet a letter you add one to its stored number.
One cell per letter key in the hash map, value = the tally
Both words have 7 letters, so start an empty tally for every letter.
What happens in this step
s = "anagram", t = "nagaram" s.length = 7 and t.length = 7, so they could still be anagrams The hash map starts empty, shown here as a dash in every cell. Different lengths would have ended the answer right here.
Steps to visualize
- Each cell below is one key in the hash map: the letter is the label and its tally is the value.
- If the two strings have different lengths, stop straight away and answer false.
- Walk through s and add one to the tally of each letter you meet.
- Walk through t and take one away from each letter. A tally that is already zero means t has a letter s does not have.
- If you finish t without running out of any letter, the two words are anagrams.
Walk through the code
Same walkthrough, now with the code. Press Next to move one step and watch which lines run.
Both words have 7 letters, so start an empty tally for every letter.
What happens in this step
s = "anagram", t = "nagaram" s.length = 7 and t.length = 7, so they could still be anagrams The hash map starts empty, shown here as a dash in every cell. Different lengths would have ended the answer right here.
Solution
function isAnagram(s, t) {
if (s.length !== t.length) {
return false;
}
const counts = new Map();
for (const char of s) {
counts.set(char, (counts.get(char) || 0) + 1);
}
for (const char of t) {
const current = counts.get(char) || 0;
if (current === 0) {
return false;
}
counts.set(char, current - 1);
}
return true;
}- Time
- O(n)
- Space
- O(1) because at most 26 letter keys are stored
Test cases
| Input | Expected | Covers |
|---|---|---|
s = "anagram", t = "nagaram" | true | example from the description |
s = "rat", t = "car" | false | same length but different letters |
s = "a", t = "ab" | false | lengths do not match, so the early exit runs |
s = "a", t = "a" | true | smallest possible input |
s = "aacc", t = "ccac" | false | same letters but used a different number of times |
s = "conversation", t = "conservation" | true | longer pair of real words |