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
相关推荐
6 分钟前
2.12矩阵问题,发牌,数字金字塔
线性代数·算法·矩阵
无聊的小坏坏12 分钟前
一文讲通:二分查找的边界处理
数据结构·c++·算法
m0_5287490012 分钟前
C语言错误处理宏两个比较重要的
java·linux·算法
TracyCoder12332 分钟前
LeetCode Hot100(50/100)——153. 寻找旋转排序数组中的最小值
算法·leetcode·职场和发展
诸葛务农32 分钟前
点云配准在人形机器人中的应用:ICP算法(2)
人工智能·算法·机器学习·机器人
摘星编程38 分钟前
**解锁Agent智能体新纪元:自主协作、任务分解与人类意图对齐的终极指南**
算法
mmz120739 分钟前
逆序对问题(c++)
c++·算法
化学在逃硬闯CS40 分钟前
Leetcode110.平衡二叉树
数据结构·c++·算法·leetcode
谢铭轩41 分钟前
题解:P8035 [COCI 2015/2016 #7] Otpor
c++·算法
listhi5201 小时前
双目立体视觉中的彩色SAD算法
算法