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
相关推荐
old_power29 分钟前
【PCL】Segmentation 模块—— 基于图割算法的点云分割(Min-Cut Based Segmentation)
c++·算法·计算机视觉·3d
Bran_Liu43 分钟前
【LeetCode 刷题】字符串-字符串匹配(KMP)
python·算法·leetcode
涛ing1 小时前
21. C语言 `typedef`:类型重命名
linux·c语言·开发语言·c++·vscode·算法·visual studio
Jcqsunny1 小时前
[分治] FBI树
算法·深度优先··分治
黄金小码农1 小时前
C语言二级 2025/1/20 周一
c语言·开发语言·算法
謓泽2 小时前
【数据结构】二分查找
数据结构·算法
00Allen003 小时前
Java复习第四天
算法·leetcode·职场和发展
攻城狮7号3 小时前
【10.2】队列-设计循环队列
数据结构·c++·算法
懒羊羊大王&4 小时前
179最大数(贪心算法)分析+源码+证明
算法·贪心算法
小小志爱学习4 小时前
提升 Go 开发效率的利器:calc_util 工具库
数据结构·算法·golang