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
相关推荐
Coovally AI模型快速验证30 分钟前
当视觉语言模型接收到相互矛盾的信息时,它会相信哪个信号?
人工智能·深度学习·算法·机器学习·目标跟踪·语言模型
电院工程师1 小时前
SIMON64/128算法Verilog流水线实现(附Python实现)
python·嵌入式硬件·算法·密码学
轮到我狗叫了1 小时前
力扣.84柱状图中最大矩形 力扣.134加油站牛客.abb(hard 动态规划+哈希表)牛客.哈夫曼编码
算法·leetcode·职场和发展
丛雨要玩游戏1 小时前
字符函数和字符串函数
c语言·开发语言·算法
八个程序员1 小时前
自定义函数(C++)
开发语言·c++·算法
ad钙奶长高高2 小时前
【C语言】初始C语言
c语言·开发语言·算法
罗西的思考2 小时前
【Agent】 ACE(Agentic Context Engineering)源码阅读笔记---(3)关键创新
人工智能·算法
报错小能手3 小时前
C++笔记(面向对象)静态联编和动态联编
开发语言·c++·算法
WBluuue3 小时前
AtCoder Beginner Contest 430(ABCDEF)
c++·算法
小肖爱笑不爱笑3 小时前
2025/11/5 IO流(字节流、字符流、字节缓冲流、字符缓冲流) 计算机存储规则(ASCII、GBK、Unicode)
java·开发语言·算法