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
相关推荐
Pniubi35 分钟前
01背包类问题-力扣416.分割等和子集
算法·leetcode·职场和发展
文丑颜不良啊11 小时前
LeetCode3041~3060题目解析
java·leetcode
罗西的思考12 小时前
【轻量化复现大模型 / KIMI K3】MTP / EAGLE‑3 模块交互式演示
人工智能·深度学习·算法
文丑颜不良啊12 小时前
LeetCode3101~3120题目解析
java·leetcode
kyle~16 小时前
模型预测控制(MPC)
算法·机器人·控制·传感器
牢姐与蒯17 小时前
cpp算法之哈希
c++·算法·哈希算法
开开心心就好18 小时前
两张表格数据对比工具,不用写函数也能查
算法·spring·智能手机·pdf·电脑·intellij-idea·音视频
电科_银尘19 小时前
【大模型算法与工程 02】线性回归与逻辑回归:从损失函数到决策边界
算法·逻辑回归·线性回归
玄昌盛不会编程20 小时前
LeetCode——2243. 计算字符串的数字和
java·算法·leetcode
文丑颜不良啊20 小时前
LeetCode3061~3080题目解析
java·leetcode