DSAHashingFrequencies

Valid Anagram Visualizer

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.

  • Length first: different lengths can never be anagrams.
  • One map is enough: increment for s, decrement for t, then every count must be 0.
  • Order is ignored: hashing collapses permutations into the same signature.

How this visualizer works

Compare listen and silent. Counts rise for the first word and fall for the second until the map is all zeros.

Interactive trace

hash_table.state

Input
Hash table
Current look-up
Match / window
Stored entry

current_action

Load an input, then press Play or Step.

live_trace.py

1def isAnagram(s, t):
2if len(s) != len(t): return False
3freq = {}
4for ch in s: freq[ch] = freq.get(ch,0)+1
5for ch in t:
6freq[ch] = freq.get(ch,0)-1
7if freq[ch] < 0: return False
8return True

stdout — decision log

// each hash probe is recorded here
Speed Ready

Pseudocode & Complexity

idea.txt

1def isAnagram(s, t):
2if len(s) != len(t): return False
3freq = {}
4for ch in s: freq[ch] = freq.get(ch,0)+1
5for ch in t:
6freq[ch] = freq.get(ch,0)-1
7if freq[ch] < 0: return False
8return True

complexity.md

OperationTime
Count s and tO(n)
Map compareO(Σ)
SpaceO(Σ)

Python implementation

solution.py

1def isAnagram(s, t):
2 if len(s) != len(t):
3 return False
4 freq = {}
5 for ch in s:
6 freq[ch] = freq.get(ch, 0) + 1
7 for ch in t:
8 freq[ch] = freq.get(ch, 0) - 1
9 if freq[ch] < 0:
10 return False
11 return True