LeetCode75——Day11

编程入门 行业动态 更新时间:2024-10-20 03:14:27

LeetCode75——Day11

LeetCode75——Day11

文章目录

    • 一、题目
    • 二、题解

一、题目

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?

二、题解

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;}
};

更多推荐

LeetCode75——Day11

本文发布于:2023-12-06 07:54:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1666936.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!