-
Notifications
You must be signed in to change notification settings - Fork 43
/
is-subsequence.py
54 lines (50 loc) · 1.14 KB
/
is-subsequence.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79568772
class Solution(object):
def isSubsequence(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
queue = collections.deque(s)
for c in t:
if not queue: return True
if c == queue[0]:
queue.popleft()
return not queue
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/79568772
class Solution(object):
def isSubsequence(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
si, ti = 0, 0
while si < len(s) and ti < len(t):
if t[ti] == s[si]:
si += 1
ti += 1
return si == len(s)
# V2
# Time: O(n)
# Space: O(1)
class Solution(object):
def isSubsequence(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
if not s:
return True
i = 0
for c in t:
if c == s[i]:
i += 1
if i == len(s):
break
return i == len(s)