面试经典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;
    }
};
相关推荐
蚊子码农1 分钟前
算法题解记录-208实现Trie前缀树
运维·服务器·算法
2301_800256112 分钟前
【人工智能引论期末复习】第3章 搜索求解2 - 对抗搜索
人工智能·算法·深度优先
江公望4 分钟前
QT/QML qmlRegisterType()函数浅谈
开发语言·qt
foundbug9995 分钟前
MATLAB中实现信号迭代解卷积功能
开发语言·深度学习·matlab
程序猿阿伟10 分钟前
《量子算法开发实战手册:Python全栈能力的落地指南》
python·算法·量子计算
wen__xvn13 分钟前
代码随想录算法训练营DAY13第六章 二叉树part01
数据结构
木子020414 分钟前
Java8集合list.parallelStream() 和 list.stream() 区别
数据结构·list
不爱吃糖的程序媛18 分钟前
OpenHarmony 通用C/C++三方库 标准化鸿蒙化适配
c语言·c++·harmonyos
fqbqrr22 分钟前
2601C++,导出控制
c++
雪风飞舞24 分钟前
python根据音频生成柱状图
开发语言·python·音视频