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];
}

编辑距离算法-DP问题的更多相关文章

  1. 用C#实现字符串相似度算法(编辑距离算法 Levenshtein Distance)

    在搞验证码识别的时候需要比较字符代码的相似度用到"编辑距离算法",关于原理和C#实现做个记录. 据百度百科介绍: 编辑距离,又称Levenshtein距离(也叫做Edit Dist ...

  2. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)

    转自:http://www.sigvc.org/bbs/forum.php?mod=viewthread&tid=981 http://www.cnblogs.com/ivanyb/archi ...

  3. Levenshtein Distance算法(编辑距离算法)

    编辑距离 编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符, ...

  4. 字符串相似度算法(编辑距离算法 Levenshtein Distance)(转)

    在搞验证码识别的时候需要比较字符代码的相似度用到“编辑距离算法”,关于原理和C#实现做个记录. 据百度百科介绍: 编辑距离,又称Levenshtein距离(也叫做Edit Distance),是指两个 ...

  5. 字符串相似度算法(编辑距离算法 Levenshtein Distance)

    在搞验证码识别的时候需要比较字符代码的相似度用到“编辑距离算法”,关于原理和C#实现做个记录.据百度百科介绍:编辑距离,又称Levenshtein距离(也叫做Edit Distance),是指两个字串 ...

  6. 自然语言处理(5)之Levenshtein最小编辑距离算法

    自然语言处理(5)之Levenshtein最小编辑距离算法 题记:之前在公司使用Levenshtein最小编辑距离算法来实现相似车牌的计算的特性开发,正好本节来总结下Levenshtein最小编辑距离 ...

  7. 编辑距离算法(Levenshtein)

    编辑距离定义: 编辑距离,又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数. 许可的编辑操作包括:将一个字符替换成另一个字符,插入一个字符,删除一个字符. 例如 ...

  8. Java实现编辑距离算法

    Java实现编辑距离算法 编辑距离,又称Levenshtein距离(莱文斯坦距离也叫做Edit Distance),是指两个字串之间,由一个转成另一个所需的最少编辑操作次数,如果它们的距离越大,说明它 ...

  9. Levenshtein distance 编辑距离算法

    这几天再看 virtrual-dom,关于两个列表的对比,讲到了 Levenshtein distance 距离,周末抽空做一下总结. Levenshtein Distance 介绍 在信息理论和计算 ...

随机推荐

  1. SpringSecurity过滤器顺序

    https://blog.csdn.net/qq_35720307/article/details/97656608 org.springframework.security.config.annot ...

  2. UML-如何进行面向对象设计?

    1.开发者如何设计对象? 1).直接编码 2).uml图,然后编码 3).uml图,不编码 绘图要轻量的 2.并行创建若干模型 如:5分钟画交互图,5分钟画类图.反复交替 3.选择什么样的UML CA ...

  3. php中openssl_pkey_get_private()函数遇到false的问题 解决办法

    今天用openssl_pkey_get_private()函数遇到了一个大坑: 如果你的私钥文件(private_key.pem)是 -----BEGIN PRIVATE KEY-----字符串字符串 ...

  4. drf序列化和反序列化

    目录 drf序列化和反序列化 一.自定义序列化 1.1 设置国际化 二.通过视图类的序列化和反序列化 三.ModelSerializer类实现序列化和反序列化 drf序列化和反序列化 一.自定义序列化 ...

  5. zxing生成二维码转base64 img直接显示 Image对象转Base64码(java)

    public static String encodeToBase64(String content){ MultiFormatWriter multiFormatWriter = new Multi ...

  6. mysql按月分表, 组合查询

    每个月月底最后一天建好下个月的空表 或每年底建1到12月的空表 , table_201901,table_201902,table_201903 增加记录不需要修改,insert到当月对应表就好了. ...

  7. echart图表demo

    <!DOCTYPE html><html><head> <title>echarts</title></head><scr ...

  8. TPO1-2 The Origin of Theater

    Stories (myths) may then grow up around a ritual. Frequently the myths include representatives of th ...

  9. python将当前时间加上7天

    datetime.datetime.now() + datetime.timedelta(days = 7)).strftime("%Y-%m-%d"

  10. Cobbler_自动装系统

    Cobbler —自动装系统的操作步骤 Cobbler是一款自动化操作系统安装的实现,与PXE安装系统的区别就是可以同时部署多个版本的系统,而PXE只能选择一种系统. Cobbler 的安装 # 在一 ...