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
相关推荐
DoraBigHead5 分钟前
小哆啦解题记 · 射箭引爆气球,怎么射最省箭?
算法
AI_Keymaker17 分钟前
手术台上的AlphaGo:约翰霍普金斯大学发布自主手术机器人
算法·机器人
AI_Keymaker18 分钟前
人类早期驯服野生机器人珍贵影像(不是)
算法·机器人
今禾1 小时前
一行代码引发的血案:new Array(5) 到底发生了什么?
前端·javascript·算法
橙几1 小时前
击败了90%的解法?Two Sum 从 O(n²) 到 O(n) 的优化之路
算法
叶子爱分享1 小时前
经典排序算法之归并排序(Merge Sort)
算法·排序算法
珹洺1 小时前
C++算法竞赛篇:DevC++ 如何进行debug调试
java·c++·算法
Norvyn_72 小时前
LeetCode|Day18|20. 有效的括号|Python刷题笔记
笔记·python·leetcode
呆呆的小鳄鱼2 小时前
leetcode:冗余连接 II[并查集检查环][节点入度]
算法·leetcode·职场和发展
墨染点香2 小时前
LeetCode Hot100【6. Z 字形变换】
java·算法·leetcode