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].

问S中有多少符合words里面的单词(可以不连续哦)

当然是二分啊,我们保存S中字母的位置,对于每个words我们都查找字母的位置,然后+1一位继续找

 class Solution {
public:
int numMatchingSubseq(string S, vector<string>& words) {
int num=;
vector<int>vec[];
int len=S.size();
for(int i=;i<len;i++){
vec[S[i]-'a'].push_back(i);
}
for(auto word:words){
int add=;
int wordLen=word.size();
int flag=;
// cout<<word<<endl;
for(int i=;i<wordLen;i++){
if(vec[word[i]-'a'].size()==){
flag=;
break;
}
auto it=lower_bound(vec[word[i]-'a'].begin(),vec[word[i]-'a'].end(),add);
if(it==vec[word[i]-'a'].end()){
flag=;
break;
}
add=(*it);
add++;
// cout<<add<<endl;
}
if(flag){
num++;
}
}
return num;
}
};

74th LeetCode Weekly Contest Valid Number of Matching Subsequences的更多相关文章

  1. 74th LeetCode Weekly Contest Valid Tic-Tac-Toe State

    A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to r ...

  2. 74th LeetCode Weekly Contest Number of Subarrays with Bounded Maximum

    We are given an array A of positive integers, and two positive integers L and R (L <= R). Return ...

  3. 74th LeetCode Weekly Contest Preimage Size of Factorial Zeroes Function

    Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...

  4. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  5. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  6. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  7. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  8. LeetCode 792. 匹配子序列的单词数(Number of Matching Subsequences)

    792. 匹配子序列的单词数 792. Number of Matching Subsequences 相似题目 392. 判断子序列

  9. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

随机推荐

  1. Gym 101142C :CodeCoder vs TopForces(强连通算法)

    题意:N个人,每个人有a属性和b属性,如果一个人的a或者b大于另外一个人,我们说这个人可以打败那个人.且这种关系可以传递.对于每个人,输出他可以打败多少人.(保证每个a不相同,保证每个b不相同. 思路 ...

  2. photon server (1)

    Photon是一套使用广泛的socket server引擎,服务端底层C++编写,客户端C#编写,跨多平台,收费,效率可观的一款引擎.实用上前有九城游戏(原魔兽世界代理),现在笔者发现多款腾讯旗下3D ...

  3. 孤独地、凄惨地AK

    一个\(OIer\)要写多少\(for\) 才能被称为一个\(OIer\) 一位巨佬要爆过多少次零 才能在省选逆袭 手指要多少次掠过键盘 才能安心地休息 \(OI\)啊 我的朋友 在风中\(AK\) ...

  4. 洛谷 P4525 & P4526 [模板] 自适应辛普森积分

    题目:https://www.luogu.org/problemnew/show/P4525 https://www.luogu.org/problemnew/show/P4526 学习辛普森积分:h ...

  5. Django_form补充

    问题1:  注册页面输入为空,报错:keyError:找不到password def clean(self): print("---",self.cleaned_data)     ...

  6. 【转】 Pro Android学习笔记(三四):Menu(5):动态菜单

    目录(?)[-] OptionsMenu的创建方式 如何再次创建OptionsMenu 每次访问都重新填充菜单项 OptionsMenu的创建方式 OptionMenu在第一次访问该菜单时调用,只调用 ...

  7. web实现本地缓存的方法

    Cookie(或者Cookies) 指一般网站为了辨别用户身份.进行session跟踪而储存在用户本地终端上的数据(通常经过加密). cookie一般通过http请求中在头部一起发送到服务器端.一条c ...

  8. top查看CPU情况

    Linux查看CPU情况 在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CPU 使用状况.运行 top ...

  9. 写一个c程序辨别系统是大端or小端字节序

    字节序有两种表示方法:大端字节序(big ending),小端字节序(little  ending) 看一个unsigned short 数据,它占2个字节,给它赋值0x1234.若采用的大端字节序, ...

  10. Fast Walsh–Hadamard transform

    考虑变换 $$\hat{A_x} = \sum_{i\ or\ x = x}{ A_i }$$ 记 $S_{t}(A,x) = \sum_{c(i,t)\ or\ c(x,t)=c(x,t),\ i ...