面试经典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;
    }
};
相关推荐
OxYGC5 分钟前
[玩转GoLang] 5分钟整合Gin / Gorm框架入门
开发语言·golang·gin
锐策9 分钟前
Lua 核心知识点详解
开发语言·lua
TNTLWT19 分钟前
单例模式(C++)
javascript·c++·单例模式
水饺编程26 分钟前
Windows 命令行:cd 命令3,当前目录,父目录,根目录
c语言·c++·windows·visual studio
天上的光26 分钟前
大模型——剪枝、量化、蒸馏、二值化
算法·机器学习·剪枝
kyle~26 分钟前
C/C++---动态内存管理(new delete)
c语言·开发语言·c++
m0_5522008241 分钟前
《UE5_C++多人TPS完整教程》学习笔记49 ——《P50 应用瞄准偏移(Applying Aim Offset)》
c++·游戏·ue5
m0_552200821 小时前
《UE5_C++多人TPS完整教程》学习笔记50 ——《P51 多人游戏中的俯仰角(Pitch in Multiplayer)》
c++·游戏·ue5
pzx_0011 小时前
【LeetCode】14. 最长公共前缀
算法·leetcode·职场和发展
self_myth1 小时前
算法与数据结构实战技巧:从复杂度分析到数学优化
算法