面试经典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;
    }
};
相关推荐
Ting.~10 分钟前
在java中接入百度地图
java·开发语言·dubbo
小短腿的代码世界12 分钟前
Qt对象树析构链与智能指针协同:零泄漏内存管理架构
开发语言·qt·架构
zhaqonianzhu19 分钟前
LOL切回桌面问题,采用监控抓出元凶方式
开发语言
Aurorar0rua22 分钟前
CS50 x 2024 Notes Arrays - 04
c语言·开发语言·学习方法
Asize22 分钟前
数组数据结构底层:从灵活到陷阱
前端·javascript·算法
John_ToDebug29 分钟前
Chromium 132→148 升级实战:Legacy IPC 消息丢失问题深度解析
c++·chrome·ai·架构
wuminyu38 分钟前
Java世界中StringTable源码剖析
java·linux·c语言·jvm·c++
hairenwangmiao1 小时前
B4041 [GESP202409 四级] 区间排序
算法·排序
一起吃元宵1 小时前
百度网盘下载不限速的办法_百度网盘不限速
开发语言·百度网盘·下载不限速·不限速·百度网盘不限速
人道领域1 小时前
【LeetCode刷题日记】47.全排列Ⅱ
java·开发语言·算法·leetcode