[LeetCode] 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.

Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?

Credits:
Special thanks to @pbrother for adding this problem and creating all test cases.

 

這道題算比較簡單的一種,我們可以用兩個指針分別指向字符串s和t,然后如果字符相等,則i和j自增1,反之只有j自增1,最后看i是否等於s的長度,等於說明s已經遍歷完了,而且字符都有在t中出現過,參見代碼如下:

 

解法一:

class Solution {
public:
    bool isSubsequence(string s, string t) {
        int i = 0;
        for (int j = 0; j < t.size() && i < s.size(); ++j) {
            if (s[i] == t[j]) ++i;
        }
        return i == s.size();
    }
};

 

題目中的 Follow up 說如果有大量的字符串s,問我們如何進行優化。那么既然字符串t始終保持不變,我們就可以在t上做一些文章。子序列雖然不需要是連着的子串,但是字符之間的順序是需要的,那么我們可以建立字符串t中的每個字符跟其位置直接的映射,由於t中可能會出現重復字符,所以把相同的字符出現的所有位置按順序加到一個數組中,所以就是用 HashMap 來建立每個字符和其位置數組之間的映射。然后遍歷字符串s中的每個字符,對於每個遍歷到的字符c,我們到 HashMap 中的對應的字符數組中去搜索,由於位置數組是有序的,我們使用二分搜索來加快搜索速度,這里需要注意的是,由於子序列是有順序要求的,所以需要一個變量 pre 來記錄當前匹配到t字符串中的位置,對於當前s串中的字符c,即便在t串中存在,但是若其在位置 pre 之前,也是不能匹配的。所以我們可以使用 uppper_bound() 來二分查找第一個大於 pre 的位置,若不存在,直接返回 false,否則將 pre 更新為二分查找的結果並繼續循環即可,參見代碼如下:

 

解法二:

// Follow up
class Solution {
public:
    bool isSubsequence(string s, string t) {
        int pre = -1, n = t.size();
        unordered_map<char, vector<int>> char2pos;
        for (int i = 0; i < n; ++i) char2pos[t[i]].push_back(i);
        for (char c : s) {
            auto it = upper_bound(char2pos[c].begin(), char2pos[c].end(), pre);
            if (it == char2pos[c].end()) return false;
            pre = *it;
        }
        return true;
    }
};

 

類似題目:

Number of Matching Subsequences

 

參考資料:

https://leetcode.com/problems/is-subsequence/

https://leetcode.com/problems/is-subsequence/discuss/87302/Binary-search-solution-for-follow-up-with-detailed-comments

https://leetcode.com/problems/is-subsequence/discuss/87408/Binary-search-solution-to-cope-with-input-with-many-Ss(with-explanation)

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM