-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubstring.py
More file actions
42 lines (38 loc) · 918 Bytes
/
Substring.py
File metadata and controls
42 lines (38 loc) · 918 Bytes
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
def isSubsequence(s: str, t: str) -> bool:
S=len(s)
T=len(t)
if s=='':
return True
if S>T:
return False
i=0
for j in range(T):
if s[i]==t[j]:
if i==S-1:
return True
i+=1
return False
'''
dp={}
# c=0
def solve(s, t, n, m, c):
if n == 0 or m==0:
#print(c)
#print(len(s))
if c == len(s):
#print("Yes")
return True
else:
return False
elif (n,m) in dp:
return dp[(n,m)]
else:
if s[n - 1] == t[m - 1]:
c=solve(s, t, n - 1, m - 1, c + 1)
else:
c=solve(s, t, n, m-1, c)
dp[(n,m)]=c
return c
return solve(s, t, len(s), len(t), 0)
'''
print(isSubsequence("abc","ahbgdc"))