Skip to content

Commit a0ce9e6

Browse files
committed
DP
1 parent 83f1a02 commit a0ce9e6

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def numDistinct(self, s: str, t: str) -> int:
3+
@lru_cache(None)
4+
def dp(i, j):
5+
if j == 0: return 1 # Case t = "", there is a valid subsequence which is empty string.
6+
if i == 0: return 0
7+
ans = dp(i - 1, j)
8+
if s[i-1] == t[j-1]:
9+
ans += dp(i - 1, j - 1)
10+
return ans
11+
12+
return dp(len(s), len(t))

0 commit comments

Comments
 (0)