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
相关推荐
金枪不摆鳍5 分钟前
hot100二分查找专题
数据结构·算法
YGGP7 分钟前
【Golang】LeetCode 54. 螺旋矩阵
算法·leetcode·矩阵
十八岁讨厌编程14 分钟前
【算法训练营 · 二刷总结篇】贪心算法、图论部分
算法·贪心算法·图论
没有医保李先生23 分钟前
嵌入式面试八股文整理(持续更新)
算法
mit6.82427 分钟前
ai五层结构
算法
F_D_Z30 分钟前
最长连续序列的长度LongestConsecutive
算法·哈希表·最长连续序列
TracyCoder12331 分钟前
LeetCode Hot100(58/100)——138. 随机链表的复制
leetcode·链表
DeepModel32 分钟前
【回归算法】广义线性模型(GLM)详解
人工智能·算法·回归
沪漂阿龙37 分钟前
大模型采样策略终极指南:Top-k、Top-p与结构化输出最佳实践
人工智能·算法·机器学习
DeepModel39 分钟前
【回归算法】局部加权回归(LWR)详解
人工智能·算法·回归