-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc-13.36.py
38 lines (30 loc) · 852 Bytes
/
c-13.36.py
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
"""
Give an efficient algorithm for determining if a pattern P is a subsequence
(not substring) of a text T. What is the running time of your algorithm?
"""
def is_subsequence(T, P): # O(n+m)
n, m = len(T), len(P)
i, j = 0, 0
while i < n and j < m:
if T[i] == P[j]:
i += 1
j += 1
else:
i += 1
return j == m
if __name__ == "__main__":
T = "this is an example text of this test"
P = "hexle ex of tt"
assert is_subsequence(T, P)
T = "this is an example text of this test"
P = "b"
assert not is_subsequence(T, P)
T = "this is an example text of this test"
P = ""
assert is_subsequence(T, P)
T = "Simpl Test Subseq"
P = "STS"
assert is_subsequence(T, P)
T = "Simpl Test Subseq"
P = "STSS"
assert not is_subsequence(T, P)