1. 程式人生 > >python leetcod 115. Distinct Subsequences

python leetcod 115. Distinct Subsequences

dp[i][j]表示s[:j]與t[:i]的匹配程度,字串的DP一般都是這樣設定,然後就是動態轉移方程的設定

class Solution:
    def numDistinct(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: int
        """
        m=len(t)
        n=len(s)
        dp=[[1]*(n+1)]
        dp+=[[0]*(n+1) for _ in range(m)]
        for
i in range(1,m+1): for j in range(1,n+1): if t[i-1]==s[j-1]: dp[i][j]=dp[i-1][j-1]+dp[i][j-1] else: dp[i][j]=dp[i][j-1] return dp[m][n]