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
相关推荐
持续学习的程序员+126 分钟前
强化学习Q-chunking算法
算法
Polaris北42 分钟前
第二十七天打卡
开发语言·c++·算法
风吹乱了我的头发~1 小时前
Day30:2026年2月20日打卡
算法
blackicexs1 小时前
第五周第五天
算法
不吃橘子的橘猫2 小时前
《集成电路设计》复习资料2(设计基础与方法)
学习·算法·fpga开发·集成电路·仿真·半导体
halen3332 小时前
How Masters Tool Fixed My Digital Disaster
算法·均值算法·推荐算法
重生之后端学习2 小时前
78. 子集
java·数据结构·算法·职场和发展·深度优先
摸鱼仙人~3 小时前
0-1背包与完全背包:遍历顺序背后的秘密
人工智能·算法
juleskk3 小时前
2.15 复试训练
开发语言·c++·算法
那起舞的日子3 小时前
斐波那契数列
java·算法