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
相关推荐
自己的九又四分之三站台1 分钟前
9:MemNet记忆层使用,实现大模型对话上下文记忆
人工智能·算法·机器学习
LXS_35718 分钟前
STL - 函数对象
开发语言·c++·算法
aini_lovee21 分钟前
基于粒子群算法(PSO)优化BP神经网络权值与阈值的实现
神经网络·算法
老鼠只爱大米30 分钟前
LeetCode经典算法面试题 #230:二叉搜索树中第K小的元素(递归法、迭代法、Morris等多种实现方案详细解析)
算法·leetcode·二叉搜索树·二叉树遍历·第k小的元素·morris遍历
星期五不见面32 分钟前
嵌入式学习!(一)C++学习-leetcode(21)-26/1/29
学习·算法·leetcode
2501_9413220338 分钟前
通信设备零部件识别与检测基于改进YOLOv8-HAFB-2算法实现
算法·yolo
modelmd1 小时前
【递归算法】汉诺塔
python·算法
2401_838472511 小时前
C++中的装饰器模式实战
开发语言·c++·算法
白中白121381 小时前
算法题-06
算法
珍珠是蚌的眼泪1 小时前
LeetCode_二叉树1
leetcode·二叉树·层序遍历·前序遍历·中序遍历·后续遍历