Binary - determined sequence

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

 

Given a string s and t, s is determined whether the sequence of t.

You can think of s and t contains only lowercase letters. T can be very long string (length = ~ 500,000), and s is a short string (length <= 100).

A string is a sequence number of the original string to delete (or may not be deleted) without changing the character string of the remaining characters of the new relative position is formed. (E.g., "ace" is "abcde" of a sequence, and "aec" instead).

示例 1:
s = "abc", t = "ahbgdc"

Return true.

示例 2:
s = "axc", t = "ahbgdc"

Return false.

Follow-up challenge:

If a large number of inputs S, referred to as S1, S2, ..., Sk where k> = 10 billion in order to check if you need them to sequences of T. In this case, what would you change the code?

 

Code:

 1 class Solution {
 2 public:
 3     vector<int> memo[26];
 4     bool isSubsequence(string s, string t) {
 5         for(int i = 0;i < t.size();++i)
 6             memo[t[i] - 'a'].push_back(i);
 7         int u = -1;
 8         for(auto p : s){
 9             int l = 0, r = memo[p - 'a'].size() - 1;
10             if(r < 0) return false;
11             while(l < r){
12                 int mid = (l + r) >> 1;
13                 if(memo[p - 'a'][mid] > u) r = mid;
14                 else l = mid + 1;
15             }
16             if(memo[p - 'a'][l] <= u) return false;
17             u = memo[p - 'a'][l];
18         }
19         return true;
20     }
21 };
View Code

 

Guess you like

Origin www.cnblogs.com/sxq-study/p/12355340.html