Skip to content

Commit 79440ae

Browse files
committed
392. Is Subsequence
1 parent 5b26ad3 commit 79440ae

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# 392. Is Subsequence
2+
3+
# Given two strings s and t, return true if s is a subsequence of t, or false otherwise.
4+
5+
# A subsequence of a string is a new string that is formed from the original string by deleting
6+
# some (can be none) of the characters without disturbing the relative positions of the remaining characters.
7+
# (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
8+
9+
10+
# Example 1:
11+
12+
# Input: s = "abc", t = "ahbgdc"
13+
# Output: true
14+
# Example 2:
15+
16+
# Input: s = "axc", t = "ahbgdc"
17+
# Output: false
18+
19+
20+
def isSubsequence(s, t):
21+
i = 0
22+
j = 0
23+
# Loop until one of the pointers reaches the end
24+
while i < len(s) and j < len(t):
25+
# If the characters match, move both pointers forward
26+
if s[i] == t[j]:
27+
i += 1
28+
j += 1
29+
# If the characters don't match, move only the pointer of t forward
30+
else:
31+
j += 1
32+
# Return true if the pointer of s reaches the end of s, false otherwise
33+
return i == len(s)
34+
35+
s ="abc"
36+
t ="ahbgdc"
37+
print(isSubsequence(s,t))

0 commit comments

Comments
 (0)