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. 2017-2018-1 20179203 《Linux内核原理与分析》第三周作业

    攥写人:李鹏举 学号:20179203 ( 原创作品转载请注明出处) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163.com/course/US ...

  2. BZOJ1799 [Ahoi2009]self 同类分布[数位DP]

    求出[a,b]中各位数字之和能整除原数的数的个数. 有困难的一道题.被迫看了题解:枚举每一个各位数字的和($<=162$),设计状态$f[len][sum][rest]$表示dp后面$len$位 ...

  3. rsync 介绍和参数说明

    Rsync 介绍: 我们经常需要在不同目录或者服务器之间做文件同步和更新,Linux提供了很多内置命令可以使用比如scp等等,但是今天我们介绍一个更加强大的工具rsync.rsync 命令是一个远程同 ...

  4. glusterfs安装配置简单使用

    GlusterFS是一种分布式分布式文件系统,默认采用无中心完全对等架构,搭建维护使用十分简单,是很受欢迎的分布式文件系统. 官网https://www.gluster.org/,官网上表示Glust ...

  5. 为什么并行测试很困难以及如何使用 ConTest 辅助测试

    众所周知并行程序设计易于产生 bug.更为严重的是,往往在开发过程的晚期当这些并行 bug 引起严重的损害时才能发现它们并且难于调试它们.即使彻底地对它们进行了调试,常规的单元测试实践也很可能遗漏并行 ...

  6. js拼的onclick调用方法需要注意的地方 之一

    1.首先,明确一点,js方法中参数可以传递字符串,对象,number类型等,对象传递的是引用,方法中修改了,会影响到方法外面的对象. 2.下面重现项目中遇到的一个问题:(其实就是要明白通过引号来拼接字 ...

  7. python 基础 字符串格式化

    print "hello %s %s" % ('wd','pc') c风格 print "hello {1} {0}".format("wd" ...

  8. 【Java】Java程序员面试宝典(第三版)第5章----Java程序设计基本概念

    1.static静态变量,在次级作用域也可以被修改. 2.k++ + k++.第一个自加实际上只有在与计算+k++时补增.详情P36的题目. 3.Java数据类型从低到高分为(byte short c ...

  9. 2017 world final

    E 解题关键:二分时注意C函数的单调性. #include<bits/stdc++.h> #define eps 1e-8 #define INF 0x3f3f3f3f using nam ...

  10. std::min error C2059: 语法错误:“::” 的解决方法

    std::min error C2059: 语法错误:"::" 的解决方法 下面这段代码: size_t n = std::min(count_, num_elements); 编 ...