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
相关推荐
做怪小疯子6 小时前
LeetCode 热题 100——矩阵——旋转图像
算法·leetcode·矩阵
努力学习的小廉6 小时前
我爱学算法之—— BFS之最短路径问题
算法·宽度优先
高山上有一只小老虎6 小时前
构造A+B
java·算法
木头左6 小时前
缺失值插补策略比较线性回归vs.相邻填充在LSTM输入层的性能差异分析
算法·线性回归·lstm
sin_hielo7 小时前
leetcode 2435
数据结构·算法·leetcode
crescent_悦7 小时前
PTA L1-020 帅到没朋友 C++
数据结构·c++·算法
鳄鱼儿7 小时前
密码算法的OID查阅
算法
lxh01138 小时前
螺旋数组题解
前端·算法·js
czlczl200209258 小时前
算法:二叉树的公共祖先
算法
稚辉君.MCA_P8_Java9 小时前
Gemini永久会员 Java动态规划
java·数据结构·leetcode·排序算法·动态规划