Skip to content

[LeetCode] 392. 判断子序列 #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Animenzzzz opened this issue Sep 7, 2019 · 0 comments
Open

[LeetCode] 392. 判断子序列 #83

Animenzzzz opened this issue Sep 7, 2019 · 0 comments

Comments

@Animenzzzz
Copy link
Owner

题目描述:

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

你可以认为 s 和 t 中仅包含英文小写字母。字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=100)。

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

示例 1:

s = "abc", t = "ahbgdc"

返回 true.

示例 2:

s = "axc", t = "ahbgdc"

返回 false.

后续挑战 :

如果有大量输入的 S,称作S1, S2, ... , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列。在这种情况下,你会怎样改变代码?

解题思路:遍历长串,遇到和短串相等,短串下标s_index加1,最后如果等于短串长度,则是子串

C++解题:

class Solution {
public:
    bool isSubsequence(string s, string t) {
        int s_index = 0;
        for (int i = 0; i < t.size(); i++)
        {
            if(s[s_index] == t[i]) s_index++;
        }
        return s_index == s.size() ? true:false;
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant