LeetCode 面试经典150题 [25/150 判断子序列]


avatar
GuoYulong 2024-06-22 186

题目描述

给定字符串 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;
    }
};

相关阅读

注意!!!

站点域名更新!!!部分文章图片等由于域名问题无法显示!!!

通知!!!

站点域名更新!!!部分文章图片等由于域名问题无法显示!!!