mobile wallpaper 1
mobile wallpaper 2
mobile wallpaper 3
mobile wallpaper 4
133 字
1 分钟
LeetCode 面试经典150题 [25/150 判断子序列]
2024-06-22
2024-06-23

题目描述#

给定字符串 s 和 t ,判断 s 是否为 t 的子序列。

字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,“ace”是”abcde”的一个子序列,而”aec”不是)。

示例 1:

输入:s = “abc”, t = “ahbgdc” 输出:true 示例 2:

输入:s = “axc”, t = “ahbgdc” 输出:false

个人C++解答#

class Solution {
public:
bool isSubsequence(string s, string t) {
int index = 0,flag = 0;
for(auto str_:s){
index = t.find(str_,index);
if(index == t.length()-1 && flag == 1) return false;
if(index == t.length()-1 && flag == 0) flag = 1;
if(index==-1) return false;
index++;
}
return true;
}
};
分享

如果这篇文章对你有帮助,欢迎分享给更多人!

LeetCode 面试经典150题 [25/150 判断子序列]
https://ericonline.cn/posts/leetcode-面试经典150题-25-150-判断子序列/
作者
Eric
发布于
2024-06-22
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时

目录