DSAHashingFrequencies

Ransom Note Visualizer

What is Ransom Note

Can you cut the note out of a magazine? Each magazine letter may be used at most once. Count the magazine, then spend one count per note letter. A missing or exhausted letter means no.

  • Magazine is the inventory. Extra leftover letters are fine.
  • Note is the shopping list. Every character must find a remaining count.
  • Fail fast on the first letter you cannot pay for.

How this visualizer works

Note aa, magazine aab. After counting a:2 b:1, the note spends two a’s and succeeds. Try note a magazine b to watch a miss.

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 canConstruct(note, mag):
2freq = {}
3for ch in mag: freq[ch] = freq.get(ch,0)+1
4for ch in note:
5if freq.get(ch,0) == 0:
6return False
7freq[ch] -= 1
8return True

stdout — decision log

// each hash probe is recorded here
Speed Ready

Pseudocode & Complexity

idea.txt

1def canConstruct(note, mag):
2freq = {}
3for ch in mag: freq[ch] = freq.get(ch,0)+1
4for ch in note:
5if freq.get(ch,0) == 0:
6return False
7freq[ch] -= 1
8return True

complexity.md

OperationTime
Count magazineO(m)
Spend noteO(n)
SpaceO(Σ)

Python implementation

solution.py

1def canConstruct(note, magazine):
2 freq = {}
3 for ch in magazine:
4 freq[ch] = freq.get(ch, 0) + 1
5 for ch in note:
6 if freq.get(ch, 0) == 0:
7 return False
8 freq[ch] -= 1
9 return True