Apps & feeds

How Autocorrect Actually Works

At its core, autocorrect compares the word you typed with every word in its dictionary. For each one, it counts the fewest letters you'd have to insert, delete or replace to get there. That count is the edit distance. The closest word wins, and a tie goes to the more common word.

Step 1 of 5 · Edges

Start from an empty word

Objective
Explain why the grid's first row and column count up from 0.
How this step gets you there
Watch the top row and left column fill. Each cell compares a word's start with nothing, so the only way there is one edit per letter.

Autocorrect scores a typo by the fewest one-letter edits that turn it into a real word. It fills a grid to find it. Row 0 and column 0 compare a word with nothing, so each holds a count of letters.

Look for: The top row and the left column fill with 0, 1, 2, 3 and up.

StopDP table
rows a, columns bfriend
0
f1
r2
e3
n4
d5

1/2Column 0: turning the first i letters of "frend" into nothing takes i deletes.

  • CH1 ↖ keep/replace
  • CH2 ↑ delete
  • CH3 ← insert

The point: Turning nothing into n letters takes n edits.

Next: Match

Step 2 of 5 · Match

Matching letters cost nothing

Objective
Explain why a cell copies its diagonal when both words end in the same letter.
How this step gets you there
Step to a cell where the letters match and look at the diagonal neighbour. The shared letter needs no edit, so nothing is added.

Every other cell compares the start of “frend” with the start of “friend”. When both end in the same letter, that letter needs no edit, so the cell copies the cell diagonally up and to the left.

Look for: The cell being filled, and its neighbour up and to the left.

StopDP table
rows a, columns bfriend
0123456
f10
r2
e3
n4
d5

1/1"f" → "f": both end in f, so keep it. dp[1][1] = dp[0][0] = 0.

  • CH1 ↖ keep/replace
  • CH2 ↑ delete
  • CH3 ← insert

The point: Same last letter: copy the diagonal.

Next: Mismatch

Step 3 of 5 · Mismatch

A mismatch costs 1 plus the cheapest neighbour

Objective
Fill a mismatched cell yourself: say which three neighbours it reads and which edit each one means.
How this step gets you there
Step through the cells where letters differ and watch the three neighbours the cell reads. The caption names the edit the cheapest one stands for.

When the last letters differ, one edit has to fix them: replace (from the diagonal), delete (from above) or insert (from the left). Take whichever neighbour is cheapest and add 1.

Look for: The three neighbours each cell reads: diagonal, up and left.

StopDP table
rows a, columns bfriend
0123456
f101
r2
e3
n4
d5

1/2"f" → "fr": f ≠ r, so 1 + min(↖ 1, ↑ 2, ← 0) = 1, by inserting r.

  • CH1 ↖ keep/replace
  • CH2 ↑ delete
  • CH3 ← insert
Check your learning · predictWhy this question: Filling the cell yourself shows you know the rule: one edit, plus the cheapest way to get the rest right. Every other cell in the grid is built the same way.“fre” → “fri”: e and i differ. The diagonal holds 0, up holds 1, left holds 1. What goes in the cell?

The point: Each cell is 1 plus the cheapest of three.

Next: Answer

Step 4 of 5 · Answer

The corner cell is the answer

Objective
Read the edit distance from the corner cell and trace the edits back.
How this step gets you there
Step to the last cell and follow the highlighted path back to the start. Each step on the path is one edit, or a free match.

The bottom-right cell compares all of “frend” with all of “friend”. Tracing the cheapest steps back from it spells out the edits, and autocorrect suggests the words with the smallest score.

Look for: The bottom-right cell, and the path traced back from it.

StopDP table
rows a, columns bfriend
0123456
f1012345
r2101234
e3211123
n4322212
d543332

1/3"frend" → "frien": d ≠ n, so 1 + min(↖ 2, ↑ 1, ← 3) = 2, by deleting d.

  • CH1 ↖ keep/replace
  • CH2 ↑ delete
  • CH3 ← insert
Check your learning · predictWhy this question: Answering before the corner fills shows you can see the edits between two words directly. That count is the score autocorrect ranks suggestions by.How many edits turn “frend” into “friend”?

The point: The smallest score wins the suggestion.

Next: All together

Step 5 of 5 · All together

Put it together

Here's the whole grid for frend and friend, cell by cell. Each chapter on the scrubber is one of the steps above. Then press Change to type two words of your own, or try a Break it case.

Edit distance, one cell at a time

StopDP table
FromTo
rows a, columns bfriend
0
f1
r2
e3
n4
d5

1/33Column 0: turning the first i letters of "frend" into nothing takes i deletes.

  • CH1 ↖ keep/replace
  • CH2 ↑ delete
  • CH3 ← insert

Go deeper

Type a word

Objective
Predict which word autocorrect suggests for a typo, and explain when edit distance alone picks the wrong one.
How this step gets you there
You type a word and the model ranks 20,000 common words by edits. Opening a suggestion shows its grid, and the swapped-letters example shows where counting edits fails.

The model scores your word against the 20,000 most common English words, most common first, and keeps the five closest. Pick a suggestion to see its grid. Try a real slip, then one that fools it: swapped letters cost two edits here, so “recieve” lands on “relieve”, one edit away, rather than “receive”.

Try

All 20,000 scored. "definitely" is the closest, 1 edit away.

StopRanking
TypeddefinatelyScored20,000 / 20,000

“definately” → “definitely”

StopDP table
rows a, columns bdefinitely
012345678910
d10123456789
e21012345678
f32101234567
i43210123456
n54321012345
a65432112345
t76543221234
e87654332123
l98765443212
y109876554321
  • CH1 ↖ keep/replace
  • CH2 ↑ delete
  • CH3 ← insert

Return dp[10][10] = 1: replace a with i.

Questions

Is this what my phone does?
It's the core. Phone keyboards also weigh which keys sit next to the one you hit and which word fits after the ones before it, and they learn the words you use. The step they all share is this one: score the dictionary by how close each word is to what you typed.
Why doesn't “teh” become “the”?
Swapping two letters counts as two edits here, one replacement for each letter. “ten” is one edit from “teh”, so it wins. Many spell checkers use Damerau–Levenshtein distance instead, which counts a swap of neighbouring letters as one edit and fixes this.
Why does “helo” become “help” and not “hello”?
Both are one edit away, and so are “held”, “hell” and “hero”. A tie goes to the most common word, and “help” is far more common than “hello”. Real spell checkers also model which slips people make most, like typing one l where there should be two.
Why doesn't it suggest my word?
It only knows the 20,000 most common English words of 10 letters or fewer. A word outside them can't be suggested, however close you get: “misspell” isn't one of them, so “mispell” becomes “spell”. Phone keyboards carry much bigger dictionaries, and learn the words you type.
What is edit distance?
The fewest single-letter inserts, deletes and replacements that turn one word into another. It's also called Levenshtein distance, after Vladimir Levenshtein, who described it in 1965. Filling the grid takes one step per cell, so two 10-letter words take 100 steps.

Practice the pattern

The grid is dynamic programming: each cell is built from answers already in the table. Word Break runs the same idea over a string and a dictionary. Coin Change takes 1 plus the cheapest earlier answer, as each edit does, and Climbing Stairs is the smallest table of all.