面试经典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;
    }
};
相关推荐
ysa0510302 分钟前
Fenwick 树进行快速统计
算法
im_AMBER14 分钟前
Leetcode 33
算法·leetcode·职场和发展
andyguo24 分钟前
全面解读大型语言模型测评:从认知演进到实操框架
人工智能·算法
张人玉31 分钟前
WPF布局控件(界面骨架核心)
开发语言·c#·wpf·布局控件
闲人编程37 分钟前
使用MLflow跟踪和管理你的机器学习实验
开发语言·人工智能·python·机器学习·ml·codecapsule
lzptouch39 分钟前
线性回归算法
算法·回归·线性回归
看兵马俑的程序员43 分钟前
RAG实现-本地PDF内容加载和切片
开发语言·python·pdf
专注前端30年1 小时前
【JavaScript】reduce 方法的详解与实战
开发语言·前端·javascript
两个人的幸福online1 小时前
php使用腾讯云服务
开发语言·php·腾讯云
无敌最俊朗@1 小时前
C++ STL Deque 高频面试题与答案
开发语言·c++