LeetCode 072 Edit Distance

题目描述

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

You have the following 3 operations permitted on a word:

a) Insert a character
b) Delete a character
c) Replace a character

分析

维基百科:Levenshtein distance
LeetCode 072 题解

  • kitten → sitten (substitution of “s” for “k”)
  • sitten → sittin (substitution of “i” for “e”)
  • sittin → sitting (insertion of “g” at the end).
  • 如果有word1长度为0,那么返回word2的长度
  • 最大返回max{word1.length, word2.lenght}
  • 只有在word1和word2字符串完全相同时,返回0

    leva,b(i,j) 表示:字符串a从1至i,与字符串b从1至j,转换所需最小步数

    则题目中所求的结果是: leva,b(|a|,|b|)

  • |a| :字符串a的长度
  • |b| :字符串b的长度

《LeetCode 072 Edit Distance》

《LeetCode 072 Edit Distance》

代码

伪代码

function LevenshteinDistance(char s[1..m], char t[1..n]):
  // for all i and j, d[i,j] will hold the Levenshtein distance between
  // the first i characters of s and the first j characters of t;
  // note that d has (m+1)*(n+1) values
  declare int d[0..m, 0..n]

  set each element in d to zero

  // source prefixes can be transformed into empty string by
  // dropping all characters
  for i from 1 to m:
      d[i, 0] := i

  // target prefixes can be reached from empty source prefix
  // by inserting every character
  for j from 1 to n:
      d[0, j] := j

  for j from 1 to n:
      for i from 1 to m:
          if s[i] = t[j]:
            d[i, j] := d[i-1, j-1]              // no operation required
          else:
            d[i, j] := minimum(d[i-1, j] + 1,   // a deletion
                               d[i, j-1] + 1,   // an insertion
                               d[i-1, j-1] + 1) // a substitution

  return d[m, n]

题目代码

    public static int minDistance(String word1, String word2) {

        if (word1.equals(word2)) {
            return 0;
        }

        if (word1.length() == 0) {
            return word2.length();
        }

        if (word2.length() == 0) {
            return word1.length();
        }

        char[] w1 = word1.toCharArray();
        char[] w2 = word2.toCharArray();

        // (m+1)*(n+1)的矩阵
        int[][] d = new int[w1.length + 1][w2.length + 1];

        // 初始化第0列
        for (int i = 0; i <= w1.length; i++) {
            d[i][0] = i;
        }

        // 初始化第0行
        for (int i = 0; i <= w2.length; i++) {
            d[0][i] = i;
        }

        for (int i = 1; i <= w1.length; i++) {
            for (int j = 1; j <= w2.length; j++) {

                if (w1[i - 1] == w2[j - 1]) {
                    d[i][j] = d[i - 1][j - 1];
                } else {
                    d[i][j] = Math.min(d[i - 1][j] + 1,// 删除操作
                            Math.min(d[i][j - 1] + 1,// 插入操作
                                    d[i - 1][j - 1] + 1));// 替换操作
                }
            }
        }

        return d[w1.length][w2.length];
    }
    原文作者:_我们的存在
    原文地址: https://blog.csdn.net/yano_nankai/article/details/48949121
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞