[LeetCode]题解(python):072-Edit Distance

题目来源:

  https://leetcode.com/problems/edit-distance/



题意分析:

  word1最少通过多少步可以变成word2。word1只能进行一下的操作。a)插入一个字符,b)删除一个字符,c)替代一个字符。比如“aba”变成“abc”只需要通过替代最后一个字符就可以达到。



题目思路:

  这很明显是一个动态规划的问题。建立一个二维数组ans,其中ans[i][j]代表word1[i:]要变成word2[j:]至少需要多少步。那么如果word1[i] == word2[j],则ans[i][j] = ans[i+1][j+1],否者,ans[i][j]= min(ans[i +1][j],ans[ans[i][j+1],ans[i+1][j+1])/分别代表每个操作/ + 1。只要处理好初始化问题就可以了。



代码(Python):

  

class Solution(object):
    def minDistance(self, word1, word2):
        """
        :type word1: str
        :type word2: str
        :rtype: int
        """
        m,n = len(word1),len(word2)
        ans = [[0 for i in range(n + 1)] for j in range(m + 1)]
        for i in range(m + 1):
            ans[i][n] = m - i
        for i in range(n + 1):
            ans[m][i] = n - i
        m -= 1;n -= 1
        while m >= 0:
            t = n
            while t >= 0:
                if word1[m] == word2[t]:
                    ans[m][t] = ans[m + 1][t + 1]
                else:
                    ans[m][t] = min(ans[m][t+1],ans[m+1][t],ans[m+1][t+1]) + 1
                t -= 1
            m -= 1
        return ans[0][0]



转载请注明出处:http://www.cnblogs.com/chruny/p/5069714.html

时间: 2024-10-11 18:22:27

[LeetCode]题解(python):072-Edit Distance的相关文章

072 Edit Distance

072 Edit Distance 纯dp 解法, 唯一可以优化的就是空间复杂度了,以下代码没有优化... class Solution: # @param {string} word1 # @param {string} word2 # @return {integer} def minDistance(self, word1, word2): m, n = len(word1), len(word2) if m*n == 0: return max(m,n) dp = [[0] * (n +

【LeetCode】161. One Edit Distance

Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if they are both one edit distance apart. Intuition 同时遍历比较S和T的字符,直至遇到不同的字符:如果S和T的字符个数相同时,跳过不同的那个字符,继续遍历:如果S和T的字符个数相差为1时,跳过较长的字符串的当天字符,继续遍历.如果剩下的字符都相等,那么返回tr

Java for 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) Repla

LeetCode之“动态规划”: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 charact

Edit Distance (or Levenshtein Distance) python solution for leetcode EPI 17.2

https://oj.leetcode.com/problems/edit-distance/ 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

[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

全部最新的题解可以在 我的 github 上找,欢迎 star 和 watch ~ 更新中~~ 说明 这个系列的题解包括用 C++/Java/Python 写的 leetcode 上的算法题目,和 Sql 写的 leetcode 上的数据库题目. 有些题目虽然 AC 了却还没写分析,所以这次就开坑来完成. 链接: 我的 github Leetcode Algorithms Problems Leetcode Database Problems CSDN 题解索引 001.Two_Sum (Med

Edit Distance leetcode java

题目: 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) R

【leetcode刷题笔记】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 characterb) Delete a characterc) Replace

[LeetCode] One Edit Distance 一个编辑距离

Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance的拓展,然而这道题并没有那道题难,这道题只让我们判断两个字符串的编辑距离是否为1,那么我们只需分下列三种情况来考虑就行了: 1. 两个字符串的长度之差大于1,那么直接返回False 2. 两个字符串的长度之差等于1,那么长的那个字符串去掉一个字符,剩下的应该和短的字符串相同 3. 两个字符串的长度之

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible