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
相关推荐
52Hz11821 分钟前
力扣230.二叉搜索树中第k小的元素、199.二叉树的右视图、114.二叉树展开为链表
python·算法·leetcode
苦藤新鸡24 分钟前
56.组合总数
数据结构·算法·leetcode
菜鸟233号35 分钟前
力扣647 回文子串 java实现
java·数据结构·leetcode·动态规划
LiLiYuan.36 分钟前
【Cursor 中找不到LeetCode 插件解决办法】
算法·leetcode·职场和发展
Charlie_lll38 分钟前
力扣解题-[3379]转换数组
数据结构·后端·算法·leetcode
captain3761 小时前
Java队列(Queue)
算法·链表
TracyCoder1231 小时前
LeetCode Hot100(23/100)——142. 环形链表 II
算法·leetcode·链表
jigsaw_zyx1 小时前
提示词工程
人工智能·算法
A尘埃1 小时前
银行个人贷款违约风险预测(逻辑回归)
算法·机器学习·逻辑回归