面试经典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;
    }
};
相关推荐
xiaoye-duck11 小时前
《C++ 项目:手写大模型接入 SDK》ChatGPT接口设计(中):全量响应实现和测试
网络·c++·语言模型·chatgpt
揽星河..11 小时前
【C语言】指针
c语言·开发语言
小郎君65811 小时前
Vue 多标签页的加载态设计:为什么我没有用 Suspense 实现 PageLoading
vue.js·算法
小小数媒成员11 小时前
UGUI原理(一)
开发语言·游戏·unity·游戏引擎
JJJennie77712 小时前
MAI Gateway私有化部署全攻略:大模型网关如何接入?附架构图
算法·gateway·ai网关·魔芋ai
速易达网络12 小时前
Three.js 动画入门教学:从场景搭建到流畅动效
开发语言·javascript·ecmascript
東隅已逝,桑榆非晚12 小时前
算法3.双指针法-快乐数
c++·笔记·学习·算法
福大大架构师每日一题12 小时前
excelize v2.11.1发布:Go 1.26.0起步,16项安全修复与嵌入对象能力全面升级
开发语言·安全·golang
Sarvartha12 小时前
集合概述 & Collection 接口
开发语言
专注仿真12 小时前
卷积神经网络(CNN)算法详解
人工智能·python·算法·代码·大模型训练