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
相关推荐
abant2几秒前
leetcode 84 单调栈
算法·leetcode·职场和发展
liuyao_xianhui1 分钟前
递归_反转链表_C++
java·开发语言·数据结构·c++·算法·链表·动态规划
CoderCodingNo1 分钟前
【GESP】C++七级考试大纲知识点梳理 (3) 图论基础与遍历算法
c++·算法·图论
深蓝轨迹4 分钟前
LeetCode105. 从前序与中序遍历序列构造二叉树
数据结构·算法
TracyCoder1235 分钟前
LeetCode Hot100(63/100)——31. 下一个排列
数据结构·算法·leetcode
智者知已应修善业17 分钟前
【不用第三变量交换2个数】2024-10-18
c语言·数据结构·c++·经验分享·笔记·算法
会编程的土豆25 分钟前
c语言时间戳从入门到精通
linux·c语言·算法
所谓伊人,在水一方33325 分钟前
【机器学习精通】第2章 | 优化算法深度解析:从梯度下降到自适应优化器
人工智能·python·算法·机器学习·信息可视化
Storynone29 分钟前
【Day24】LeetCode:122. 买卖股票的最佳时机 II,55. 跳跃游戏,45. 跳跃游戏II,1005. K次取反后最大化的数组和
python·算法·leetcode
滴滴答滴答答31 分钟前
机考刷题之 17&18&19&20&21&22 LeetCode 1248&121&43&93&62&63
算法·leetcode·职场和发展