THINK FIRST·CODE LATER

← All labs

Edit distance (spell-checker distance)

Problem

The edit distance between two words is the fewest single-character insertions, deletions and substitutions that turn one into the other — the measure behind "did you mean…?".

Write EditDistance. For each pair of words, fill the table d[i][j] (distance between the first i letters of a and the first j letters of b), print the distance, then trace back one optimal list of operations, and — when both words have at most 8 letters — the last row of the table.

For the input

kitten sitting

the program prints

kitten -> sitting: 3 edit(s)
  operations: replace k>s; replace e>i; insert g;
  last row: 6 6 5 4 3 3 2 3

Several optimal operation lists can exist, so the operations: line is not checked by the tests (the distance and the last row are).

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

d8-5-data-1.txt

kitten sitting
What your program must print for d8-5-data-1.txt
kitten -> sitting: 3 edit(s)

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < d8-5-data-1.txt

Windows PowerShell does not understand <:

Get-Content d8-5-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.