Skip to main content
1 of 2
D.W.
  • 165.5k
  • 21
  • 230
  • 489

One other trick: if you find a match at edit distance one, you can immediately stop without searching for any other matches -- that's the best one you'll ever find.

Why? The edit distance is a non-negative integer, so the only smaller edit distance possible is zero. However, you already checked that the word is not in the dictionary (that was your first bullet point), so edit distance is zero. That means that as soon as you find a distance-one match, you know you'll never find anything better.

As mentioned in my comment to your prior question, there's lots written on spelling correction and this problem. There are many data structures, algorithms, and tricks documented in the literature and online. I suggest you spend some quality time doing some research and familiarizing yourself with them.

D.W.
  • 165.5k
  • 21
  • 230
  • 489