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


avatar
GuoYulong 2024-06-22 136

题目描述

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

相关阅读

注意!!!

新增会员中心页面,方便管理个人账户,充值功能暂不开启,请勿为本网站进行任何充值活动!!!

通知!!!

① 过年好!!!拖更几个月了已经,年后继续更新!!