Edit distance (Levenshtein distance) measures the minimum number of single-character operations needed to transform one string into another. It is widely used in spell checking, DNA sequence alignment, and natural language processing for measuring string similarity.
Given two strings s1 and s2, compute the minimum edit distance using three operations: insert, delete, and replace (each with cost 1).
Create a (m+1) x (n+1) DP table where m and n are the lengths of s1 and s2. Initialize the first row and column with incremental values (base cases for empty string).
If the characters match, no operation is needed:
Where dp[i-1][j] is delete, dp[i][j-1] is insert, and dp[i-1][j-1] is replace.
Return the minimum edit distance as an integer.
Input: s1 = "kitten", s2 = "sitting"
Output: 3
Explanation: Two replacements and one insertion transform kitten into sitting.
Input: s1 = "horse", s2 = "ros"
Output: 3
Initialize the first DP row and column with distances from an empty prefix.
Use the diagonal value for matching characters; otherwise add one to the minimum neighboring state.
Sign in to take notes on this problem
Accepts: string
Accepts: string
Edit distance (Levenshtein distance) measures the minimum number of single-character operations needed to transform one string into another. It is widely used in spell checking, DNA sequence alignment, and natural language processing for measuring string similarity.
Given two strings s1 and s2, compute the minimum edit distance using three operations: insert, delete, and replace (each with cost 1).
Create a (m+1) x (n+1) DP table where m and n are the lengths of s1 and s2. Initialize the first row and column with incremental values (base cases for empty string).
If the characters match, no operation is needed:
Where dp[i-1][j] is delete, dp[i][j-1] is insert, and dp[i-1][j-1] is replace.
Return the minimum edit distance as an integer.
Input: s1 = "kitten", s2 = "sitting"
Output: 3
Explanation: Two replacements and one insertion transform kitten into sitting.
Input: s1 = "horse", s2 = "ros"
Output: 3
Initialize the first DP row and column with distances from an empty prefix.
Use the diagonal value for matching characters; otherwise add one to the minimum neighboring state.
Sign in to take notes on this problem
Accepts: string
Accepts: string