标签:distance string number transform 编辑 算法 minimum need DP
Levenshtein Distance
The Levenshtein distance is a string metric for measuring the difference between two sequences. Informally, the Levenshtein distance between two words is the minimum number of single-character edits (insertions, deletions or substitutions) required to change one word into the other.
Example
For example, the Levenshtein distance between kitten
and sitting
is 3
, since the following three edits change one into the other, and there is no way to do it with fewer than three edits:
- kitten → sitten (substitution of "s" for "k")
- sitten → sittin (substitution of "i" for "e")
- sittin → sitting (insertion of "g" at the end)
思路:
Let’s take a simple example of finding minimum edit distance between strings ME
and MY
. Intuitively you already know that minimum edit distance here is 1
operation and this operation. And it is replacing E
with Y
. But let’s try to formalize it in a form of the algorithm in order to be able to do more complex examples like transforming Saturday
into Sunday
.
To apply the mathematical formula mentioned above to ME → MY
transformation we need to know minimum edit distances of ME → M
, M → MY
and M → M
transformations in prior. Then we will need to pick the minimum one and add one operation to transform last letters E → Y
. So minimum edit distance of ME → MY
transformation is being calculated based on three previously possible transformations.
To explain this further let’s draw the following matrix:
- Cell
(0:1)
contains red number 1. It means that we need 1 operation to transformM
to an empty string. And it is by deletingM
. This is why this number is red. - Cell
(0:2)
contains red number 2. It means that we need 2 operations to transformME
to an empty string. And it is by deletingE
andM
. - Cell
(1:0)
contains green number 1. It means that we need 1 operation to transform an empty string toM
. And it is by insertingM
. This is why this number is green. - Cell
(2:0)
contains green number 2. It means that we need 2 operations to transform an empty string toMY
. And it is by insertingY
andM
. - Cell
(1:1)
contains number 0. It means that it costs nothing to transformM
intoM
. - Cell
(1:2)
contains red number 1. It means that we need 1 operation to transformME
toM
. And it is by deletingE
. - And so on...
This looks easy for such small matrix as ours (it is only 3x3
). But here you may find basic concepts that may be applied to calculate all those numbers for bigger matrices (let’s say a 9x7
matrix for Saturday → Sunday
transformation).
According to the formula you only need three adjacent cells (i-1:j)
, (i-1:j-1)
, and (i:j-1)
to calculate the number for current cell (i:j)
. All we need to do is to find the minimum of those three cells and then add 1
in case if we have different letters in i
's row and j
's column.如果等的话,找最小就好。
代码如下:
/** * @param {string} a * @param {string} b * @return {number} */ export default function levenshteinDistance(a, b) { // Create empty edit distance matrix for all possible modifications of // substrings of a to substrings of b. const distanceMatrix = Array(b.length + 1).fill(null).map(() => Array(a.length + 1).fill(null)); // Fill the first row of the matrix. // If this is first row then we're transforming empty string to a. // In this case the number of transformations equals to size of a substring. for (let i = 0; i <= a.length; i += 1) { distanceMatrix[0][i] = i; } // Fill the first column of the matrix. // If this is first column then we're transforming empty string to b. // In this case the number of transformations equals to size of b substring. for (let j = 0; j <= b.length; j += 1) { distanceMatrix[j][0] = j; } for (let j = 1; j <= b.length; j += 1) { for (let i = 1; i <= a.length; i += 1) { const indicator = a[i - 1] === b[j - 1] ? 0 : 1; distanceMatrix[j][i] = Math.min( distanceMatrix[j][i - 1] + 1, // deletion distanceMatrix[j - 1][i] + 1, // insertion distanceMatrix[j - 1][i - 1] + indicator, // substitution ); } } return distanceMatrix[b.length][a.length]; }
标签:distance,string,number,transform,编辑,算法,minimum,need,DP 来源: https://www.cnblogs.com/Archer-Fang/p/10551450.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。