[LeetCode]392. Is Subsequence (Easy) Python-2021.02.01/2023.09.22
Posted on 2023-09-22 13:38 Uriel 閱讀(44) 評論(0) 編輯 收藏 引用 所屬分類: 閑來無事重切Leet Code 、大水題判斷s是不是t的子串(字符不需要連續),水題
1 #392
2 #Runtime: 8 ms (Beats 94.61%)
3 #Memory: 13.3 MB (Beats 96%)
4
5 class Solution(object):
6 def isSubsequence(self, s, t):
7 """
8 :type s: str
9 :type t: str
10 :rtype: bool
11 """
12 p = 0
13 for ch in s:
14 while p < len(t) and t[p] != ch:
15 p += 1
16 if p >= len(t):
17 return False
18 if t[p] == ch:
19 p += 1
20 return True
2 #Runtime: 8 ms (Beats 94.61%)
3 #Memory: 13.3 MB (Beats 96%)
4
5 class Solution(object):
6 def isSubsequence(self, s, t):
7 """
8 :type s: str
9 :type t: str
10 :rtype: bool
11 """
12 p = 0
13 for ch in s:
14 while p < len(t) and t[p] != ch:
15 p += 1
16 if p >= len(t):
17 return False
18 if t[p] == ch:
19 p += 1
20 return True