一.题目:
有两个字符串s和t,其中s是短字符串,t是长字符串。判断s是不是t的子字符串。注意,这里应该是保留相对顺序的子字符串,也就是在s中出现的两个字符a,b应该在s中t中的相对次序相同。
Example 1:
s = “abc”, t = “ahbgdc”
Return true.
Example 2:
s = “axc”, t = “ahbgdc”
Return false.
二.解题思路:
使用队列存储短字符串,然后遍历长字符串,当与队首元素有匹配的字母时,队首元素出队列.最后判断队列中是否有元素剩余.
class Solution(object):
def isSubsequence(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
que = collections.deque(s)
for i in t:
if not que:
return True
if i==que[0]:
que.popleft()
return not que