Levenshtein Edit Distance
Compare insertion, deletion, and substitution costs for the current prefix pair.
Core Intuition & How It Works
Levenshtein edit distance computes the minimum number of insertions, deletions, and substitutions required to transform one string into another using 2D dynamic programming over string prefixes.
In the Byte Sized Algorithms interactive classroom, you can step forward and backward through this algorithm with your keyboard while watching the visual diagram, memory state, and highlighted pseudocode update together.
Time & Space Complexity
| Metric | Complexity |
|---|---|
| Time Complexity | O(m × n) |
| Space Complexity | O(m × n) or O(min(m, n)) |
| Topic Chapter | Chapter 13: Dynamic Programming |
| Difficulty Level | Advanced |
Key Pattern to Recognize
When s1[i-1] === s2[j-1], cost is dp[i-1][j-1]; otherwise take 1 + min(insert, delete, replace).
Common Pitfalls & Edge Cases
Forgetting to initialize base cases dp[i][0] = i and dp[0][j] = j representing deletions/insertions from an empty string.
Real-World & Technical Interview Applications
Spell-checkers, fuzzy search engines, OCR error correction, and bioinformatics alignment.