Distinct Subsequences LeetCode solution in Python
1# Runtime: 1092 ms (Top 41.44%) | Memory: 70.6 MB (Top 63.39%)2class Solution:3def numDistinct(self, s: str, t: str) -> int:4if len(t) > len(s):5return 06ls, lt = len(s), len(t)7res = 08dp = [[0] * (ls + 1) for _ in range(lt + 1)]9for j in range(ls + 1):10dp[-1][j] = 111for i in range(lt - 1, -1, -1):12for j in range(ls - 1, -1, -1):13dp[i][j] = dp[i][j + 1]14if s[j] == t[i]:1516dp[i][j] += dp[i + 1][j + 1]17return dp[0][0]