792. Number of Matching Subsequences
Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of S.
Example :
Input:
S = "abcde"
words = ["a", "bb", "acd", "ace"]
Output: 3
Explanation: There are three words in words that are a subsequence of S: "a", "acd", "ace".
Note:
- All words in words and S will only consists of lowercase letters.
- The length of S will be in the range of [1, 50000].
- The length of words will be in the range of [1, 5000].
- The length of words[i] will be in the range of [1, 50].
Approach
- 题目大意是判断words数组中有多少个是S的子序列,数据量比较大,所以不能用暴力枚举一遍,所以我们用映射加二分解决,我们先把S的字符进行映射并记录每个下标,然后我们枚举words,对words中每个字符串枚举字符,进行二分查找,判断下标是否有比上一个字符的下标大。
- upper_bound:查找第一个大于x的数值。
- lower_bound:查找第一个大于等于的x的数值。
Code
class Solution {
public:
int numMatchingSubseq(string S, vector<string>& words) {
vector<vector<int>>alpha(26);
for (int i = 0; i < S.size(); i++) {
alpha[S[i] - 'a'].push_back(i);
}
int res = 0;
for (string &word : words) {
bool found = true;
int x = -1;
for (char &c : word) {
int t = c - 'a';
auto it = upper_bound(alpha[t].begin(), alpha[t].end(), x);
if (it == alpha[t].end()) {
found = false;
break;
}
else {
x = *it;
}
}
if (found)res++;
}
return res;
}
};
Again
- 看到大神的播客,发现还可以只用映射的方式处理,需要慢慢理解,用一个二维数组
alpha[i][S[i]-'a']
表示当下标在此时,最近的S[i]-'a'
的下标是多少。 - 代码使用java写的,我改写了C++一直爆内存,可能是因为语言的限制吧,我觉得这方法很好,可以用来拓展我的思维。
Code
class Solution {
public int numMatchingSubseq(String S, String[] words) {
int n = S.length();
int[][] dp = new int[n + 1][32];
for (int i = 0; i < n + 1; ++i) Arrays.fill(dp[i], -1);
for (int i = n - 1; i >= 0; --i) dp[0][S.charAt(i) - 'a'] = i + 1;
for (int j = n - 2; j >= 0; --j) {
for (int i = 0; i < 32; ++i) {
dp[j + 1][i] = dp[j + 2][i];
}
dp[j + 1][S.charAt(j + 1) - 'a'] = j + 2;
}
int cnt = 0;
for (String word : words) {
int prv = 0;
boolean ok = true;
for (int j = 0; j < word.length(); ++j) {
int nxt = dp[prv][word.charAt(j) - 'a'];
if (nxt != -1) {
prv = nxt;
}
else {
ok = false;
break;
}
}
if (ok) cnt ++;
}
return cnt;
}
}