Leetcode刷題筆記-兩個字符串比較dp

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章