-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumSubsequences.py
More file actions
23 lines (20 loc) · 858 Bytes
/
Copy pathnumSubsequences.py
File metadata and controls
23 lines (20 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""
Find the number of occurrences of given subsequence in the string.
"""
def numSubsequences(string, sequence):
matrix = [[0 for a in range(len(string)+1)] for b in range(len(sequence)+1)]
for a in range(len(string)+1):
matrix[0][a] = 1
for a in range(1, len(string)+1):
for b in range(1, len(sequence)+1):
# Initialize next value with everything you've found with the given sequence.
val = matrix[b][a-1]
# If they match, everything from matrix[b-1][a-1] now becomes valid.
if string[a-1] == sequence[b-1]:
val += matrix[b-1][a-1]
matrix[b][a] = val
return matrix[len(sequence)][len(string)]
# Prints 2, 4, 6
print(numSubsequences("hotdog", "ho"))
print(numSubsequences("GeeksforGeeks", "Gks"))
print(numSubsequences("numsubsequences", "ses"))