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
相关推荐
云泽8081 小时前
C++ 可调用对象通关指南:深度解析 Lambda 表达式、function 包装器与 bind 绑定器
开发语言·c++·算法
wlsh152 小时前
Go 迭代器
算法
语戚2 小时前
力扣 3161. 块放置查询:线段树解法(Java 实现)
java·算法·leetcode·面试·线段树·力扣·
CS创新实验室3 小时前
从顺序表到动态数组:数据结构的永恒基石与现代语言的优雅封装
数据结构·算法
Black蜡笔小新3 小时前
自动化AI算法训练服务器DLTM训推一体化平台助力农业生产管理实现安全智能化
人工智能·算法·自动化
8Qi84 小时前
LeetCode 23. 合并 K 个升序链表 —— 小顶堆(PriorityQueue)
数据结构·算法·leetcode·链表·
QiLinkOS4 小时前
《打破“用爱发电”:一种基于 Gitee 与时间戳的开源权益分配机制探索》
c语言·数据结构·c++·科技·算法·gitee·开源
松间听晚5 小时前
Agentic RL 环境和代码学习:以HGPO为例
算法
智者知已应修善业5 小时前
【51单片机用T0定时器方式1,实现0.5S的时间间隔实现第一次一个灯亮、第二次二个灯亮,直到全部灯亮,然后重复整个过程】2023-12-29
c++·经验分享·笔记·算法·51单片机
小许同学记录成长5 小时前
几何体编辑与布尔运算
算法·无人机