leetcode python3 判断子序列

代码思路:利用双指针思想,逐一比较两字符串中各个字符是否相等,若s是t的子序列,则指向s的指针必会到达s末尾。

class Solution:
    def isSubsequence(self, s: str, t: str) -> bool:
        i,j=0,0
        res=False
        while i<len(s) and j<len(t):
            if s[i]==t[j]:
                i+=1
                j+=1
            else:j+=1
        if i==len(s):
            res=True
        return res

发布了30 篇原创文章 · 获赞 0 · 访问量 304

猜你喜欢

转载自blog.csdn.net/m0_37656366/article/details/105110007