• 72. Edit Distance


    package LeetCode_72
    
    /**
     * 72. Edit Distance
     * https://leetcode.com/problems/edit-distance/description/
     *
     * Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.
    You have the following 3 operations permitted on a word:
    1.Insert a character
    2.Delete a character
    3.Replace a character
    
    Example 1:
    Input: word1 = "horse", word2 = "ros"
    Output: 3
    Explanation:
    horse -> rorse (replace 'h' with 'r')
    rorse -> rose (remove 'r')
    rose -> ros (remove 'e')
    
    Example 2:
    Input: word1 = "intention", word2 = "execution"
    Output: 5
    Explanation:
    intention -> inention (remove 't')
    inention -> enention (replace 'i' with 'e')
    enention -> exention (replace 'n' with 'x')
    exention -> exection (replace 'n' with 'c')
    exection -> execution (insert 'u')
     * */
    class Solution {
        fun minDistance(word1: String, word2: String): Int {
            //method 1:Time complexity is O(3^m), Space complexity is O(m*n),
            //because has overlapping sub problems, for example editDistance(str1,str2,1,1) will call more time
            val m = word1.length
            val n = word2.length
            //return editDistance(word1, word2, m, n)
            //method 2: dp, Time complexity is O(m*n), Space complexity is O(m*n),
            return editDistanceDP(word1, word2, m, n)
        }
    
        private fun editDistanceDP(str1: String, str2: String, m: Int, n: Int): Int {
            val dp = Array(m + 1) { Array(n + 1, { 0 }) }
            /*
            There are following two different ways to store the values so that these values can be reused:
            * a) Memoization (Top Down)
              b) Tabulation (Bottom Up)
              https://www.geeksforgeeks.org/overlapping-subproblems-property-in-dynamic-programming-dp-1/
            * */
            //fill dp array by bottom up manner
            for (i in 0 .. m) {
                for (j in 0 .. n) {
                    //if first string is empty,
                    if (i == 0) {
                        dp[i][j] = j
                    } else if (j == 0) {
                        //the second string is empty,
                        dp[i][j] = i
                    } else if (str1[i - 1] == str2[j - 1]) {
                        //if last character of two string are same, ignore last character
                        dp[i][j] = dp[i - 1][j - 1]
                    } else {
                        dp[i][j] = 1 + Math.min(
                            //insert and remove
                            Math.min(dp[i][j-1], dp[i-1][j])
                            //replace
                            , dp[i - 1][j - 1]
                        )
                    }
                }
            }
            return dp[m][n]
        }
    
        private fun editDistance(str1: String, str2: String, m: Int, n: Int): Int {
            //if first string is empty,
            //the only option is insert into all chars of second string into the first
            if (m == 0) {
                return n
            }
            if (n == 0) {
                return m
            }
            //if last character of two string are same, ignore last character
            if (str1[m - 1] == str2[n - 1]) {
                return editDistance(str1, str2, m - 1, n - 1)
            }
            //compute minimum cost for all three operations
            return 1 + Math.min(
                //insert and remove
                Math.min(editDistance(str1, str2, m, n - 1), editDistance(str1, str2, m - 1, n)),
                //replace
                editDistance(str1, str2, m - 1, n - 1)
            )
        }
    }
  • 相关阅读:
    10 个十分难得的 javascript 开发经验
    牛人推荐的跨浏览器兼容性总结
    不可不看!CSS3中三十一种选择器用法
    jQuery制作鼠标经过显示图片大图,生成图片tips效果
    解决IE6中 PNG图片透明的终极方案-八种方案!
    关于JavaScript代码的执行效率总结
    【我们开发有力量之二】利用javascript制作批量网络投票机器人(自动改IP)
    网页制作中最有用的免费Ajax和JavaScript代码库
    纯javascript代码实现浏览器图片选择预览、旋转、批量上传
    悲催的IE6 七宗罪大吐槽(带解决方法)第二部分
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/12831165.html
Copyright © 2020-2023  润新知