-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIs_Subsequence.py
More file actions
59 lines (52 loc) · 1 KB
/
Is_Subsequence.py
File metadata and controls
59 lines (52 loc) · 1 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
54
55
56
57
58
59
# -*- coding:utf-8 -*-
#
# Author : TangHanYi
# E-mail : thydeyx@163.com
# Create Date : 2016-11-24 11:39:42 PM
# Last modified : 2016-11-24 11:59:09 PM
# File Name : Is_Subsequence.py
# Desc :
"""
class Solution(object):
def isSubsequence(self, s, t):
s = '$' + s
t = '$' + t
sn = len(s)
tn = len(t)
P = [0 for i in range(sn)]
j = 0
for i in range(2, sn):
while j > 0 and s[i] != s[j + 1]:
j = P[j]
if s[i] == s[j + 1]:
j += 1
P[i] = j
j = 0
for i in range(1, tn):
while j > 0 and t[i] != s[j + 1]:
j = P[j]
if t[i] == s[j + 1]:
j += 1
if j == sn - 1:
print i - sn + 1
j = P[j]
return False
"""
class Solution(object):
def isSubsequence(self, s, t):
sn = len(s)
tn = len(t)
if sn == 0:
return True
j = 0
for i in range(tn):
if t[i] == s[j]:
j += 1
if j == sn:
return True
return False
if __name__ == "__main__":
s = Solution()
st = "abc"
t = "diejfabcldjfabc"
print s.isSubsequence(st, t)