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
相关推荐
Orange--Lin4 分钟前
【用deepseek和chatgpt做算法竞赛】——还得DeepSeek来 -Minimum Cost Trees_5
人工智能·算法·chatgpt
01_11 分钟前
力扣hot100 ——搜索二维矩阵 || m+n复杂度优化解法
算法·leetcode·矩阵
SylviaW0813 分钟前
python-leetcode 35.二叉树的中序遍历
算法·leetcode·职场和发展
篮l球场15 分钟前
LeetCodehot 力扣热题100
算法·leetcode·职场和发展
pzx_00127 分钟前
【机器学习】K折交叉验证(K-Fold Cross-Validation)
人工智能·深度学习·算法·机器学习
BanLul27 分钟前
进程与线程 (三)——线程间通信
c语言·开发语言·算法
qy发大财1 小时前
分发糖果(力扣135)
数据结构·算法·leetcode
haaaaaaarry1 小时前
【分治法】线性时间选择问题
数据结构·算法
CS创新实验室2 小时前
计算机考研之数据结构:P 问题和 NP 问题
数据结构·考研·算法
OTWOL2 小时前
【C++编程入门基础(一)】
c++·算法