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
相关推荐
jing-ya19 小时前
day51 图论part3
数据结构·算法·深度优先·图论
AI浩19 小时前
UTPTrack:迈向简单统一的视觉跟踪令牌剪枝
算法·机器学习·剪枝
nananaij20 小时前
【LeetCode-02 最小偶倍数 python解法】
python·算法·leetcode
倾心琴心20 小时前
【agent辅助pcb routing coding学习】实践4 kicad pcb 核心类层次关系
算法·agent·pcb·eda·routing
im_AMBER20 小时前
Leetcode 139 最后一个单词的长度 | 找出字符串中第一个匹配项的下标
开发语言·算法·leetcode
Frostnova丶20 小时前
(6)LeetCode.42 接雨水
数据结构·算法·leetcode
像污秽一样20 小时前
算法设计与分析-习题4.4
数据结构·算法·排序算法·深度优先
x_xbx20 小时前
LeetCode:102. 二叉树的层序遍历
算法·leetcode
2401_8898846620 小时前
嵌入式C++测试框架
开发语言·c++·算法
月明长歌20 小时前
【码道初阶-Hot100】LeetCode 128. 最长连续序列:从排序双指针扫描到 HashSet,一文讲透为什么 O(n) 解法要用哈希
算法·leetcode·哈希算法