-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path392.py
33 lines (23 loc) · 855 Bytes
/
392.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
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
if len(s) == 0:
return True
if len(s) > len(t):
return False
s_pointer = 0
for t_pointer in range(len(t)):
if t[t_pointer] == s[s_pointer]:
s_pointer += 1
if s_pointer == len(s):
return True
return False
# more compact solution using the same principles in a while loop
# removes the need for checking edge cases at the top of the function
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
s_pointer, t_pointer = 0, 0
while t_pointer < len(t) and s_pointer < len(s):
if s[s_pointer] == t[t_pointer]:
s_pointer += 1
t_pointer += 1
return s_pointer == len(s)