-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistinct-subsequences.py
More file actions
53 lines (51 loc) · 1.48 KB
/
distinct-subsequences.py
File metadata and controls
53 lines (51 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 6 20:52:40 2022
@author: patha
"""
class Solution(object):
def numDistinct(self, s, t):
"""
:type s: str
:type t: str
:rtype: int
"""
m = len(s)
n = len(t)
if m < n:
return 0
if m == n:
if s == t:
return 1
return 0
val = []
for j in range(n):
val_row = []
for i in range(m):
val_row.append(0)
val.append(val_row)
for i in range(n - 1, -1, -1):
# print(val)
for j in range(m - 1, -1, -1):
if m - j < n - i:
continue
elif i == n - 1 and j == m - 1:
if t[i] == s[j]:
val[i][j] = 1
elif i == n - 1:
if t[i] == s[j]:
val[i][j] = 1 + val[i][j + 1]
else:
val[i][j] = val[i][j + 1]
elif m - j == n - i:
# print('here', t[i], s[j])
if t[i] == s[j]:
# print('here')
val[i][j] = val[i + 1][j + 1]
else:
if s[j] == t[i]:
val[i][j] = val[i + 1][j + 1] + val[i][j + 1]
else:
val[i][j] = val[i][j + 1]
# print(val)
return val[0][0]