[LeetCode]题解(python):115-Distinct Subsequences

题目来源:

  https://leetcode.com/problems/distinct-subsequences/



题意分析:

  给定字符串S和T,判断S中可以组成多少个T,T是S的子串。



题目思路:

  这是一个动态规划的问题。设定ans[i][j]为s[:i] 组成t[:j]的个数。那么动态规划方程为,if s[i - 1] == t[j - 1]: ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j];else: ans[i][j] = ans[i - 1][j]。



代码(python):

  

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

时间: 2024-10-25 13:11:00

[LeetCode]题解(python):115-Distinct Subsequences的相关文章

115 Distinct Subsequences

115 Distinct Subsequences 这道题是dp, 我定义的dp[i][j] 为用t[:i] 和s[:j] 的disitinct subsequence排列数 class Solution: # @param {string} s # @param {string} t # @return {integer} def numDistinct(self, s, t): m, n = len(t), len(s) if m == 0 or n == 0: return 0 dp,cu

115. Distinct Subsequences(js)

115. Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of S which equals T. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the charac

Leetcode 115 Distinct Subsequences 解题报告

Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solution Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from

leetcode 115. Distinct Subsequences

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative

leetcode 115 Distinct Subsequences ----- java

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative

Java for LeetCode 115 Distinct Subsequences【HARD】

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative

LeetCode之“动态规划”:Distinct Subsequences

题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing th

[leedcode 115] Distinct Subsequences

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative

115. Distinct Subsequences (String; DP)

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative

115. Distinct Subsequences *HARD* -- 字符串不连续匹配

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative