Medium Algorithms String Algorithms
Explain the edit distance (Levenshtein distance) algorithm.
Answer
Edit distance measures minimum operations (insert, delete, substitute) to transform one string to another. DP solution: dp[i][j] = distance between first i chars of s1 and first j chars of s2. If chars match, dp[i][j] = dp[i-1][j-1]. Otherwise, dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) for delete, insert, substitute respectively. Time and space O(mn). Used in spell checkers and DNA sequence alignment.
IIT Certified
Master These Concepts with IIT Certification
175+ hours of industry projects. Get placed at Bosch, Tata Motors, L&T and 500+ companies.
Relevant for Roles
Software Engineer Algorithm Developer NLP Engineer