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
相关推荐
vim怎么退出20 分钟前
43.验证二叉搜索树
前端·leetcode
2301_817031651 小时前
C语言-- 深入理解指针(4)
c语言·开发语言·算法
·醉挽清风·1 小时前
学习笔记—双指针算法—移动零
c++·笔记·学习·算法
几点才到啊1 小时前
使用 malloc 函数模拟开辟一个 3x5 的整型二维数组
数据结构·算法
编程绿豆侠2 小时前
力扣HOT100之链表:23. 合并 K 个升序链表
算法·leetcode·链表
Ayanami_Reii2 小时前
Leetcode837.新21点
c++·笔记·算法
我想进大厂2 小时前
图论---最大流(Dinic)
算法·深度优先·图论
brzhang2 小时前
效率神器!TmuxAI:一款无痕融入终端的AI助手,让我的开发体验翻倍提升
前端·后端·算法
songx_995 小时前
算法设计与分析7(贪心算法)
算法
aigonna5 小时前
Kimi 7B 语音转文字
算法