首页 > 代码库 > Leetcode Is Subsequence

Leetcode Is Subsequence

本题的思路比较简答,设置一个计数器,在scan list t的过程中,每发现一个s里面的字母,计数器就+1。如果计数器等于s的长度,说明已经找到一个subsequence 等于s。

 1 class Solution(object):
 2     def isSubsequence(self, s, t):
 3         """
 4         :type s: str
 5         :type t: str
 6         :rtype: bool
 7         """
 8         if not s:
 9             return True
10         i = 0
11         for c in t:
12             if c == s[i]:
13                 i += 1
14             if i == len(s):
15                 return True
16         
17         return False

 

Leetcode Is Subsequence