hash_table.state
Input
Hash table
Current look-up
Match / window
Stored entry
What is a Valid Anagram
Two strings are anagrams when they use the same characters the same number of times. A frequency map is a hash table from character → count. If both maps match, the strings are rearrangements of each other.
s, decrement for t, then every count must be 0.listen and silent. Counts rise for the first word and fall for the second until the map is all zeros.Interactive trace
Pseudocode & Complexity
| Operation | Time |
|---|---|
| Count s and t | O(n) |
| Map compare | O(Σ) |
| Space | O(Σ) |
Python implementation