LeetCode-792. Number of Matching Subsequences

Description

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

1.All words in words and S will only consists of lowercase letters.
2.The length of S will be in the range of [1, 50000].
3.The length of words will be in the range of [1, 5000].
4.The length of words[i] will be in the range of [1, 50].

Solution 1(C++)

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 (const auto& word : words) {
            int x = -1;
            bool found = true;

            for (char c : word) {
                auto it = upper_bound (alpha[c - 'a'].begin (), alpha[c - 'a'].end (), x);
                if (it == alpha[c - 'a'].end ()) found = false;
                else x = *it;
            }

            if (found) res++;
        }

        return res;
    }
};

算法分析

要注意,单纯的暴力算法会超时,所以需要先遍历一般S,记录26个字母出现的各自位置,然后去搜索words中,每一个word中各个字符出现的位置。

程序分析

upper_bound与lower_bound都是在左闭右开的区间中进行的二分查找算法:

  • upper_bound:返回的是被查序列中第一个大于查找值的指针,也就是返回指向被查值>查找值的最小指针。或者说:对于给定的已经排好序的a,key最晚能插入到那个位置
  • lower_bound:返回的是被查序列中第一个大于等于查找值的指针,也就是返回指向被查值>=查找值的最小指针。或者说:对于给定的已经排好序的a,key最早能插入到那个位置
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值