关闭

leetcode392

标签: leetcodestring
72人阅读 评论(0) 收藏 举报
分类:

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4889次
    • 积分:603
    • 等级:
    • 排名:千里之外
    • 原创:59篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条