Leetcode 392. Is Subsequence

Problem

Given two strings s and t, return true if s is a subsequence of t, or false otherwise.

A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).

Algorithm

Use two pointers to follow the two strings.

Code

python3 复制代码
class Solution:
    def isSubsequence(self, s: str, t: str) -> bool:
        sIndex, tIndex, sLen, tLen = 0, 0, len(s), len(t)
        while sIndex < sLen and tIndex < tLen:
            if s[sIndex] == t[tIndex]:
                sIndex += 1
                tIndex += 1
            else:
                tIndex += 1
        
        return sIndex >= sLen
相关推荐
啊阿狸不会拉杆15 分钟前
《机器学习》 第 9 章 - 深度强化学习
人工智能·算法·机器学习·计算机视觉·ai·ml
仰泳的熊猫24 分钟前
题目 1429: 蓝桥杯2014年第五届真题-兰顿蚂蚁
数据结构·c++·算法·蓝桥杯
苦藤新鸡30 分钟前
35.LRU缓存(最久未访问)问题
算法·链表·缓存
Yupureki32 分钟前
《算法竞赛从入门到国奖》算法基础:入门篇-分治
c语言·开发语言·数据结构·c++·算法·贪心算法
充值修改昵称35 分钟前
数据结构基础:B*树B+树的极致优化
数据结构·b树·python·算法
one____dream37 分钟前
【算法】相同的树与对称二叉树
b树·python·算法·递归
e疗AI产品之路39 分钟前
心电分析诊断算法评估方法介绍
算法·心电分析
爱编码的傅同学39 分钟前
【今日算法】LeetCode 11.盛水最多的容器 15.三数之和 283.移动0
数据结构·算法·leetcode
啊我不会诶39 分钟前
Codeforces Round 1072 (Div. 3)补题
笔记·学习·算法
重生之我是Java开发战士39 分钟前
【算法日记】Set 与 Map 经典算法
算法