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
相关推荐
Desirediscipline1 小时前
#define _CRT_SECURE_NO_WARNINGS 1
开发语言·数据结构·c++·算法·c#·github·visual studio
范纹杉想快点毕业1 小时前
C语言550例编程实例说明
算法
小O的算法实验室2 小时前
2026年SEVC SCI2区,面向无人机路径规划的领域专用算子进化算法,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
weixin_477271692 小时前
同人象:(两军停战谈判结盟的现场直播)马王堆帛书《周易》原文及甲骨文还原周朝生活现象《函谷门
算法·图搜索算法
nudt_qxx2 小时前
CUDA编程模型与硬件执行层级对应关系
linux·人工智能·算法
m0_531237172 小时前
C语言-分支与循环语句练习2
c语言·开发语言·算法
AIpanda8882 小时前
什么是AI销冠系统和AI提效软件系统?主要区别和应用场景是什么?
算法
程序员酥皮蛋3 小时前
hot 100 第三十三 33.排序链表
数据结构·算法·链表
蚊子码农3 小时前
算法题解记录-2452距离字典两次编辑以内的单词
开发语言·算法·c#
重生之后端学习3 小时前
207. 课程表
java·数据结构·算法·职场和发展·深度优先