Codacoda
Back to Academy

dynamic programming

Edit Distance

Edit Distance (Levenshtein Distance) computes the minimum number of single-character operations (insertions, deletions, and substitutions) required to transform one string into another. A 2D DP table is built where dp[i][j] represents the edit distance between the first i characters of word1 and the first j characters of word2. This is fundamental to many string comparison applications.

Use Cases

  • Spell checkers and autocorrect systems for suggesting corrections
  • DNA sequence comparison and mutation analysis in bioinformatics
  • Fuzzy string matching in search engines and record linkage

Complexity Analysis

MetricBestAverageWorst
TimeO(mn)O(mn)O(mn)
SpaceO(mn)

Visualization

BDCAB
0
0
0
0
0
0
A
0
0
0
0
0
0
B
0
0
0
0
0
0
C
0
0
0
0
0
0
B
0
0
0
0
0
0
D
0
0
0
0
0
0
Speed:1x
LCS of "ABCBD" and "BDCAB"Step 1 / 23

Implementation

Output

Click "Run Code" to see output...