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
相关推荐
AI科技星1 天前
光速的几何本质与运动极限:基于张祥前统一场论对光子及有质量粒子运动的统一诠释
数据结构·人工智能·经验分享·算法·计算机视觉
没有bug.的程序员1 天前
负载均衡的真正含义:从算法到架构的深度解析
java·jvm·算法·微服务·架构·负载均衡
谈笑也风生1 天前
经典算法题型之复数乘法(一)
数据结构·算法
剪一朵云爱着1 天前
PAT 1056 Mice and Rice
算法·pat考试
星火开发设计1 天前
快速排序详解:原理、C++实现与优化技巧
java·c++·算法·排序算法·快速排序·知识
一分之二~1 天前
回溯算法--全排列
c语言·数据结构·c++·算法·leetcode
sali-tec1 天前
C# 基于halcon的视觉工具VisionTool Halcon发布
人工智能·深度学习·算法·计算机视觉·分类
ohnoooo91 天前
251211算法 搜索
数据结构·算法
CodeByV1 天前
【算法题】二分
算法
想唱rap1 天前
哈希(C++)
服务器·开发语言·c++·算法·哈希算法