给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
你可以认为 s 和 t 中仅包含英文小写字母。字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=100)。
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。
示例 1:
s = "abc", t = "ahbgdc"
返回 true.
示例 2:
s = "axc", t = "ahbgdc"
返回 false.
双指针法
# -*- coding: utf-8 -*-
class Solution(object):
def isSusequence(self,s,t):
"""
:type s: str
:type t: str
:rtype: bool
"""
point_s = 0
point_t = 0
while point_s < len(s) and point_t < len(t):
if s[point_s] == t[point_t]:
point_s = point_s + 1
point_t = point_t + 1
return point_s == len(s)
if __name__ == "__main__":
sol = Solution()
s = input()
t = input()
print(sol.isSusequence(s,t))