判断字符串数组中有多少字符串是某个字符串的子串

792. Number of Matching Subsequences

Medium

49939FavoriteShare

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".
class Solution {
public:
    int numMatchingSubseq(string S, vector<string>& words) {
        vector<vector<int>> Position(26);
        int Result = 0;
        for(int i = 0;i < S.length();i++){
            Position[S[i] - 'a'].push_back(i);
        }
        for(int i = 0;i < words.size();i ++){
            int PrePosition = -1;
            bool Flag = true;
            for(auto c : words[i]){
                auto iter = upper_bound(Position[c - 'a'].begin(),Position[c - 'a'].end(),PrePosition);
                if(iter == Position[c - 'a'].end()){
                    Flag = false;break;
                }
                else{
                    PrePosition = *iter;
                }
            }
            if(Flag == true){Result ++;}
        }
        return Result;
    }
};

 心得:

upper_bound和lower_bound的用法

        利用容器vector将字符串中每一个字符的位置保存下来,之后依次与数组中的字符串进行位置匹配,如果没有那么该字符串不是主字符串的位置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值