File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 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 ))
You can’t perform that action at this time.
0 commit comments