Ekinnn

芝士就是力量~✌️

leetcode392

1、 Is Subsequence
Given a string s and a string t, check if s is subsequence of t.

You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100).

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, “ace” is a subsequence of “abcde” while “aec” is not).

Example 1:
s = “abc”, t = “ahbgdc”

Return true.

Example 2:
s = “axc”, t = “ahbgdc”

Return false.
思路与简单字符串匹配几乎一样~~感觉这里不需要使用KMP吧,毕竟不需要连着相同~

class Solution {
public:
    bool isSubsequence(string s, string t) {
        int length1=s.size();
        int length2=t.size();
        int i=0,j=0;
        while(i<length1&&j<length2){
            if(s[i]==t[j]){
                ++i;
                ++j;
            }
            else
                ++j;
        }
        if(i==length1)
            return true;
        else
            return false;
    }
};
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Ekinnn/article/details/53996660
文章标签: leetcode string
个人分类: bj+string
上一篇leetcode349&amp;350
下一篇leetcode136
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭