Skip to content

Commit ba3d012

Browse files
authored
Create 115-Distinct-Subsequences.py
1 parent 6169a6f commit ba3d012

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

115-Distinct-Subsequences.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def numDistinct(self, s: str, t: str) -> int:
3+
cache = {}
4+
5+
for i in range(len(s) + 1):
6+
cache[(i, len(t))] = 1
7+
for j in range(len(t)):
8+
cache[(len(s), j)] = 0
9+
10+
for i in range(len(s) - 1, -1, -1):
11+
for j in range(len(t) - 1, -1, -1):
12+
if s[i] == t[j]:
13+
cache[(i, j)] = cache[(i+1,j+1)] + cache[(i+1,j)]
14+
else:
15+
cache[(i,j)] = cache[(i+1,j)]
16+
return cache[(0,0)]

0 commit comments

Comments
 (0)