Leetcode刷题笔记-两个字符串比较dp

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Sengo_GWU/article/details/88374775

72. Edit Distance

f(i, j) := minimum cost (or steps) required to convert first i characters of word1 to first j characters of word2

class Solution(object):
    def minDistance(self, word1, word2):
        """
        :type word1: str
        :type word2: str
        :rtype: int
        """
        n1, n2 = len(word1), len(word2)
        dp = [[0] * (n2+1) for _ in xrange(n1+1)]
                
        for i in xrange(n1+1):
            dp[i][0] = i
        
        for j in xrange(n2+1):
            dp[0][j] = j
            
        for i in xrange(1, n1+1):
            for j in xrange(1, n2+1):
                if word1[i-1] == word2[j-1]:
                    dp[i][j] = dp[i-1][j-1]
                else:
                    dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1
        
        return dp[-1][-1]
        

97. Interleaving String

 丢人啊,这题不难第一次解又没有想到AC的解法,又是超时。

DP:

class Solution(object):
    def isInterleave(self, s1, s2, s3):
        """
        :type s1: str
        :type s2: str
        :type s3: str
        :rtype: bool
        """
        n1, n2, n3 = len(s1), len(s2), len(s3)
        if n1 + n2 != n3:
            return False
        # dp[i][j] s1前i个字符串, s2前j个字符串, s3前i+j个字符串 是否能符合
        dp = [[0] * (n2+1) for _ in xrange(n1+1)]  # 先n2再n1 。。
        for i in xrange(n1+1):
            for j in xrange(n2+1):
                if i == 0 and j == 0:
                    dp[0][0] = True  # s1 empty s2 empty  s3 empty
                elif i == 0: # s1 is empty
                    dp[i][j] = dp[i][j-1] and s2[j-1] == s3[i+j-1]
                elif j == 0:  # s2 is empty
                    dp[i][j] = dp[i-1][j] and s1[i-1] == s3[i+j-1]
                else:
                    dp[i][j] = (dp[i-1][j] and s1[i-1] == s3[i+j-1]) or (dp[i][j-1] and s2[j-1] == s3[i+j-1])
        return dp[-1][-1]

10. Regular Expression Matching 

这题要再过一遍,不看思路的做

参考:1.https://leetcode.com/problems/regular-expression-matching/discuss/5651/Easy-DP-Java-Solution-with-detailed-Explanation

2.https://blog.csdn.net/hk2291976/article/details/51165010 图

class Solution(object):
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        n1, n2 = len(s), len(p)
        dp = [[False for x in xrange(n2+1)] for _ in xrange(n1+1)]  # 加1 因为最开始是空和空匹配是True 
        dp[0][0] = True
        
        for j in xrange(n2): 
            if p[j] == '*' and dp[0][j-1]:  # 星号的 减两位检查 等于 a* = empty
                dp[0][j+1] = True
        
        for i in xrange(n1):
            for j in xrange(n2):
                if p[j] == '.':  
                    dp[i+1][j+1] = dp[i][j]  # i+1, j+1 是因为一开始是空和空匹配 i=0和j=0时候
                
                elif p[j] == s[i]:
                    dp[i+1][j+1] = dp[i][j]
                
                elif p[j] == '*':
                    if p[j-1] != s[i] and p[j-1] != '.':
                        dp[i+1][j+1] = dp[i+1][j-1]
                    else:
                        dp[i+1][j+1] = dp[i+1][j] or dp[i][j+1] or dp[i+1][j-1]
        
        return dp[-1][-1]

583. Delete Operation for Two Strings

经典题

Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.

Example 1:

Input: "sea", "eat"
Output: 2
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
class Solution(object):
    def minDistance(self, word1, word2):
        """
        :type word1: str
        :type word2: str
        :rtype: int
        """
        # len - length of max common str  
        l1, l2 = len(word1), len(word2)
        
        dp = [[0]*(l2+1) for i in xrange(l1+1)]  # find the common lenght
        longest = 0
        for i in xrange(l1):
            for j in xrange(l2):
                if word1[i] == word2[j]:
                    dp[i+1][j+1] = dp[i][j] + 1
                else:
                    dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])
           
        return l1 + l2 - dp[l1][l2]*2

猜你喜欢

转载自blog.csdn.net/Sengo_GWU/article/details/88374775