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
相关推荐
月落归舟8 分钟前
排序算法---(三)
数据结构·算法·排序算法
njidf13 分钟前
C++中的观察者模式
开发语言·c++·算法
2301_8227828217 分钟前
C语言数组通关攻略!从一维到字符数组,零基础也能轻松掌握
c语言·算法·数组·编程基础·避坑技巧
zhugby22 分钟前
标号法原理
算法
努力学习的小廉1 小时前
我爱学算法之——动态规划(一)
算法·动态规划
篮l球场1 小时前
前 K 个高频元素
数据结构·算法·leetcode
汉克老师1 小时前
GESP5级C++考试语法知识(十一、递归算法(一))
c++·算法·记忆化搜索·递归算法·递归优化
qq_148115371 小时前
C++网络编程(Boost.Asio)
开发语言·c++·算法
2301_804215412 小时前
内存映射文件高级用法
开发语言·c++·算法
爱喝白开水a2 小时前
春节后普通程序员如何“丝滑”跨行AI:不啃算法,也能拿走AI
java·人工智能·算法·spring·ai·前端框架·大模型