-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path#0392 Is Subsequence.py
More file actions
32 lines (26 loc) · 962 Bytes
/
#0392 Is Subsequence.py
File metadata and controls
32 lines (26 loc) · 962 Bytes
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
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
"""
Check if string s is a subsequence of string t.
Args:
s (str): The subsequence to check.
t (str): The main sequence in which to check for the subsequence.
Returns:
bool: True if s is a subsequence of t, False otherwise.
"""
len_s = len(s)
len_t = len(t)
# If s is longer than t, s cannot be a subsequence of t
if len_s > len_t:
return False
i = 0
j = 0
# Iterate through both strings
while i < len_s and j < len_t:
# If characters match, move to the next character in s
if s[i] == t[j]:
i += 1
# Always move to the next character in t
j += 1
# If we have iterated through all characters of s, it is a subsequence
return i == len_s