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:

  1. kitten → sitten (substitution of "s" for "k")
  2. sitten → sittin (substitution of "i" for "e")
  3. 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 → MM → 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 transform M to an empty string. And it is by deleting M. This is why this number is red.
  • Cell (0:2) contains red number 2. It means that we need 2 operations to transform ME to an empty string. And it is by deleting E and M.
  • Cell (1:0) contains green number 1. It means that we need 1 operation to transform an empty string to M. And it is by inserting M. 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 to MY. And it is by inserting Y and M.
  • Cell (1:1) contains number 0. It means that it costs nothing to transform M into M.
  • Cell (1:2) contains red number 1. It means that we need 1 operation to transform ME to M. And it is by deleting E.
  • 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 + ).fill(null).map(() => Array(a.length + ).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 = ; i <= a.length; i += ) {
distanceMatrix[][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 = ; j <= b.length; j += ) {
distanceMatrix[j][] = j;
} for (let j = ; j <= b.length; j += ) {
for (let i = ; i <= a.length; i += ) {
const indicator = a[i - ] === b[j - ] ? : ;
distanceMatrix[j][i] = Math.min(
distanceMatrix[j][i - ] + , // deletion
distanceMatrix[j - ][i] + , // insertion
distanceMatrix[j - ][i - ] + indicator, // substitution
);
}
} return distanceMatrix[b.length][a.length];
}

最新文章

  1. js异步编程技巧一
  2. sqlite like 通配符 ,匹配区分大小写(默认不区分大小写)
  3. Chapter 1: 随机事件及其概率
  4. 【poj1655】Balancing Act
  5. android user版本默认开启调试模式
  6. phpcms筛选功能
  7. PHPcms怎么调用二级栏目
  8. ajax 请求二进制流 图片 文件 XMLHttpRequest 请求并处理二进制流数据 之最佳实践
  9. java include包含指令例子
  10. Linux操作系统分析__破解操作系统的奥秘
  11. 【Web】CGI与Servlet技术对比
  12. Java魔法堂:JVM的运行模式 (转)
  13. CSS 3 过渡效果之jquery 的fadeIn ,fadeOut
  14. 使用domain模块捕获异步回调中的异常
  15. 微信小程序(二)登录授权实现
  16. matplotlib坐标轴刻度-【老鱼学matplotlib】
  17. mysql 储存过程
  18. Spring配置从配置文件读取属性值
  19. Java_常遇问题(一)
  20. centos6下安装php7的memcached扩展

热门文章

  1. iTOP-4418开发板TF卡烧写-引导uboot
  2. IaaS SaaS PaaS区别
  3. 为什么Web前端变的越来越复杂,变得更加难学了
  4. dht算法原理描述
  5. 使用spark
  6. python与mysql部分函数和控制流语法对比
  7. 894A. QAQ#(暴力)
  8. iOS 之keychain详解(附有Demo)
  9. Jupyter_Notebook
  10. 简单的使用httpclient读取网页html例子