</> MAANG.io
coding interview · 101

Foundations

Master coding interviews with comprehensive coverage of data structures, algorithms, and problem-solving techniques. Progress from fundamentals to advanced topics with expertly curated content.

0/255 solved 0% complete

Hashing Fundamentals

What is this?

This is where you learn the three everyday jobs a hash structure does. Use a set to check "have I seen this before?", a Counter to answer "how many of each do I have?", and a canonical key to lump together things that are secretly the same (like words that are anagrams). Each one replaces a slow, item-by-item comparison with one quick pass.

flowchart TD A["Hashing Fundamentals"] --> B["Set for distinctness"] A --> C["Counter for how many of each"] A --> D["Canonical key for grouping"] D --> E["Equivalent items share one key"]

💡 Fun fact: The "group things that are secretly the same" trick has a famous twist — to detect anagrams you can multiply a prime number per letter, since every word maps to a unique product. This is essentially the Fundamental Theorem of Arithmetic, a 2000-year-old idea from Euclid, repurposed as a hash key.

🔓 The 5 problems in this chapter are free. Sign in with Google or Microsoft to start solving.


Core idea: Before the clever "what to store" tricks, master the three everyday uses of a hash structure: a set to test distinctness, a Counter to tally "how many of each", and a canonical key to group items that are secretly the same. Each turns an O(n²) scan into one O(n) pass.


The three reflexes

The three reflexes: distinctness uses a set (have I seen it?, O(1) membership); how many? uses a Counter (value to count, O(1) tally); equivalence uses a canonical key (normalize then group, O(1) bucketing)

Reflex Trigger phrase Tool
Set "distinct / unique / seen" set()
Frequency "how many of each / most common / counts" collections.Counter
Canonical key "group the ones that are the same under …" dict keyed by a normalized form

The five problems

Hashing fundamentals branches into five problems: Distribute Candies (set: distinct count), Min Steps to Make Anagram (count diff), Bulls and Cows (two count tables), Longest Harmonious Subsequence (count adjacent values), Group Shifted Strings (canonical key)

  • Distribute Candies — a set gives the distinct-type count; answer min(distinct, n//2).
  • Minimum Steps to Make Anagram — subtract two frequency tables; the deficit is the answer.
  • Bulls and Cows — bulls by position, then count the leftover digits and sum min of the two tallies for cows.
  • Longest Harmonious Subsequencecount values; for each v, combine count[v] + count[v+1].
  • Group Shifted Strings — build a canonical key (mod-26 difference tuple) and group by it.

The pattern

Counting collapses "compare every element to every other" into "tally once, then read the tallies." A set answers membership in O(1). And when items are equivalent under some transformation (a shift, a sort, a rotation), compute a canonical form and let the map group them — the same trick behind Group Anagrams.


📓 Draw it yourself

  1. Two tally tables. For Bulls and Cows or the anagram problem, draw both frequency tables and read the answer off the per-letter differences.
  2. Canonical key. For Group Shifted Strings, write a few strings and their difference-tuple keys; watch equivalent ones land in the same bucket.

Snap photos and embed them with the /host-diagrams skill.


Key takeaways

  • Three reflexes: set (distinct), Counter (how many), canonical key (group equivalents).
  • Counting beats nested comparison — tally once in O(n), then answer questions off the tallies.
  • A canonical key turns "are these equivalent?" into "do they hash the same?"
  • Why it matters: these are the bread-and-butter hash moves every harder problem composes from.

Order: Distribute Candies → Min Steps to Make Anagram → Bulls and Cows → Longest Harmonious Subsequence → Group Shifted Strings.

Minimum Steps to Make Two Strings Anagram

Core idea: Two strings are anagrams when they have the same letter counts. So compare the two count tables and ask one question per letter: how many copies does t still owe s? The total deficit is the number of replacements — you never need to think about positions at all.

Problem, rephrased

You manage a print shop with a tray of movable letter tiles. You currently have the tiles spelling t, and a customer wants a layout that is an anagram of their target word s (same letters, any order). The two words are the same length, so you never add or remove a tile — you only ever swap one tile out for a different letter.

Each swap is one step: pick any tile in t and overwrite it with any letter you like. Question: what's the fewest swaps to make t an anagram of s?

Formally: given two strings s and t of equal length over lowercase letters, in one step you may replace any character of t with any other character. Return the minimum number of steps so that t becomes an anagram of s.

s t Output Why
"bab" "aba" 1 t has two a but s needs two b; fix one tile
"leetcode" "practice" 5 t is short on e,d,o and has surplus p,r,a,i,c
"anagram" "mangaar" 0 already an anagram — same letter counts
"xxyyzz" "xxyyzz" 0 identical strings are trivially anagrams

Note the output is a count of edits, not the edited string and not the positions.

Sign in to continue reading

The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.

Sign in to read the full lesson

Sign in to MAANG.io

Use your Google or Microsoft account — no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.