LeetCode 792. Number of Matching Subsequences C++

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

  1. 题目大意是判断words数组中有多少个是S的子序列,数据量比较大,所以不能用暴力枚举一遍,所以我们用映射加二分解决,我们先把S的字符进行映射并记录每个下标,然后我们枚举words,对words中每个字符串枚举字符,进行二分查找,判断下标是否有比上一个字符的下标大。
  2. upper_bound:查找第一个大于x的数值。
  3. 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

  1. 看到大神的播客,发现还可以只用映射的方式处理,需要慢慢理解,用一个二维数组alpha[i][S[i]-'a']表示当下标在此时,最近的S[i]-'a'的下标是多少。
  2. 代码使用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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值