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
相关推荐
ygming8 分钟前
Q43- code973- 最接近原点的 K 个点 + Q44- code347- 前 K 个高频元素
前端·算法
lightqjx9 分钟前
【数据结构】顺序表(sequential list)
c语言·开发语言·数据结构·算法
ygming10 分钟前
Hashmap/ Hashset- Q39~Q42内容
前端·算法
蒟蒻小袁1 小时前
力扣面试150题--全排列
算法·leetcode·面试
mit6.8241 小时前
[Backlog] 核心协调器 | 终端用户界面(TUI)实现 | 多分支任务冲突解决 | 测试验证体系
人工智能·算法
SKYDROID云卓小助手1 小时前
无人设备遥控器之无线电频率篇
服务器·网络·单片机·嵌入式硬件·算法
十八岁讨厌编程1 小时前
【算法训练营Day11】二叉树part1
算法
緈福的街口2 小时前
【leetcode】2236. 判断根节点是否等于子节点之和
算法·leetcode·职场和发展
祁思妙想3 小时前
【LeetCode100】--- 1.两数之和【复习回滚】
数据结构·算法·leetcode
薰衣草23333 小时前
一天两道力扣(2)
算法·leetcode