面试经典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;
    }
};
相关推荐
武子康2 分钟前
Java-169 Neo4j CQL 实战速查:字符串/聚合/关系与多跳查询
java·开发语言·数据库·python·sql·nosql·neo4j
一只小灿灿6 分钟前
深入解析 Maven 与 Gradle:Java 项目构建工具的安装、使用
java·开发语言·maven
树在风中摇曳9 分钟前
C语言 | 文件操作详解与实战示例
c语言·开发语言
njsgcs35 分钟前
excel提取长宽,进行排版导出ezdxf 装箱算法 贪婪 总利用率91%
开发语言·python·excel
lsx20240642 分钟前
Memcached stats sizes 命令详解
开发语言
渡我白衣43 分钟前
字符串的陷阱与艺术——std::string全解析
网络·c++·人工智能·自然语言处理·智能路由器·信息与通信·caffe
吃不饱的得可可1 小时前
C++17常用新特性
开发语言·c++
_OP_CHEN1 小时前
算法基础篇:(七)基础算法之二分算法 —— 从 “猜数字” 到 “解难题” 的高效思维
c++·算法·蓝桥杯·二分查找·acm·二分答案·二分算法
西游音月1 小时前
(4)框架搭建:Qt实战项目之主窗体介绍
开发语言·qt