面试经典150题——Day26

文章目录

一、题目

392. Is Subsequence

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).

Example 1:

Input: s = "abc", t = "ahbgdc"

Output: true

Example 2:

Input: s = "axc", t = "ahbgdc"

Output: false

Constraints:

0 <= s.length <= 100

0 <= t.length <= 104

s and t consist only of lowercase English letters.

Follow up: Suppose there are lots of incoming s, say s1, s2, ..., sk where k >= 109, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code?

题目来源: leetcode

二、题解

cpp 复制代码
class Solution {
public:
    bool isSubsequence(string s, string t) {
        int n1 = s.length();
        int n2 = t.length();
        if(n1 > n2) return false;
        int index = 0;
        for(int i = 0;i < n2;i++){
            if(s[index] == t[i]) index++;
        }
        return index == n1;
    }
};
相关推荐
碧海银沙音频科技研究院11 分钟前
基于炬芯ATS308X的LE aduio LC3编解码算法功能实现
算法
小蒋观天下13 分钟前
厂区人员徘徊游荡判别——基于时序轨迹速度的逗留行为分级算法落地
大数据·人工智能·深度学习·算法·机器学习·ai大模型
程序员清风18 分钟前
堆与优先队列:从 Top K 到任务调度
java·数据结构·算法
shaibdoio18 分钟前
瞬维AI技术拆解:AI获客智能体的双层漏斗算法原理
大数据·人工智能·算法
宛 禾29 分钟前
Qt 控件入门:QLabel 从拖拽到对象树,一篇讲透
c++·qt·学习
suhan30 分钟前
【数据结构】四、串:串string、模式匹配BF\KMP、广义表
数据结构·后端·算法
hetao173383741 分钟前
联盟场-提高组-R7 & ZYZSC-S-Round 11
c++·算法
程序喵大人43 分钟前
【C++入门】编译链接模型 - 07 链接器怎样把符号对上号
开发语言·c++·编译器·编译链接·链接器
啵啵啵鱼1 小时前
第二章 线性表
java·数据结构·算法
虾..1 小时前
计算机图形学:几何表示
算法