792. Number of Matching Subsequences
Given string
S
and a dictionary of wordswords
, find the number ofwords[i]
that is a subsequence ofS
.
Example :
Input:
S = "abcde"
words = ["a", "bb", "acd", "ace"]
Output: 3
Explanation: There are three words inwords
that are a subsequence ofS
: "a", "acd", "ace".
Note:
- All words in
words
andS
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: Array. [C++]
class Solution {
public:
int numMatchingSubseq(string S, vector<string>& words) {
vector<const char*> waiting[128];
for (auto &w : words)
waiting[w[0]].push_back(w.c_str());
for (char c : S) {
auto advance = waiting[c];
waiting[c].clear();
for (auto it : advance)
waiting[*++it].push_back(it);
}
return waiting[0].size();
}
};
Approach #2: [Java]
class Solution {
public int numMatchingSubseq(String S, String[] words) {
List<Integer[]>[] waiting = new List[128];
for (int c = 0; c <= 'z'; ++c)
waiting[c] = new ArrayList();
for (int i = 0; i < words.length; ++i)
waiting[words[i].charAt(0)].add(new Integer[]{i, 1});
for (char c : S.toCharArray()) {
List<Integer[]> advance = new ArrayList(waiting[c]);
waiting[c].clear();
for (Integer[] a : advance)
waiting[a[1] < words[a[0]].length() ? words[a[0]].charAt(a[1]++) : 0].add(a);
}
return waiting[0].size();
}
}
Reference:
https://leetcode.com/problems/number-of-matching-subsequences/discuss/117634/Efficient-and-simple-go-through-words-in-parallel-with-explanation
792. Number of Matching Subsequences的更多相关文章
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- leetcode 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 ...
- LeetCode 792. 匹配子序列的单词数(Number of Matching Subsequences)
792. 匹配子序列的单词数 792. Number of Matching Subsequences 相似题目 392. 判断子序列
- [Swift]LeetCode792. 匹配子序列的单词数 | Number of Matching Subsequences
Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of ...
- [LeetCode] Number of Matching Subsequences 匹配的子序列的个数
Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of ...
- 74th LeetCode Weekly Contest Valid Number of Matching Subsequences
Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- linux系统命令笔记
一.linux系统目录 /bin 系统命令目录 /dev 设备目录 /home 每个系统用户在home下都有一个目录, 每个用户登录到系统后会自动登录到这个目录下, root用户会在/root文件夹下 ...
- [PHP]require include
- linux下安装php php-fpm(转载)
centos安装php php-fpm 1.下载php源码包http://www.php.net/downloads.php2 .安装phptar -xvf php-5.5.13.tar.bz2cd ...
- 向Ubuntu的Dash中添加图标
首先准备.xpm图标文件,如果程序文件夹中没有,那么可以根据自己喜好到网上下载喜欢的图标,不要太大,然后将其改为.xpm文件(直接改了后缀名就行).然后打开/usr/share/application ...
- mysql 执行多线程临时方案
sqr::IDatabase *db=NULL;IDbConnection *conn = NULL;int main(int argc, char* argv[]) { db = GetDataba ...
- How Autofs Works
How Autofs Works Autofs is a client-side service that automatically mounts the appropriate file syst ...
- jetty无法即时更新html、js、css等静态文件的解决办法
网上搜了一大堆方法.最常见的是找到jetty\etc\webdefault.xml文件,找到useFileMappedBuffer参数,把true改为false <init-param> ...
- ubuntu系统下安装pyspider:搭建pyspider服务器新手教程
首先感谢“巧克力味腺嘌呤”的博客和Debian 8.1 安装配置 pyspider 爬虫,本人根据他们的教程在ubuntu系统中进行了实际操作,发现有一些不同,也出现了很多错误,因此做此教程,为新手服 ...
- Linux教程:基础+中级+运维高级
视频内容40G:Linux基础视频.Linux中级视频.Linux运维高级视频+赠送 职业素质视频 +查用服务器安卓文档 目录 Linux基础教程81节 常用命令.文件管理命令详解.bash脚本编程. ...
- python3.4用循环往mysql5.7中写数据并输出
#!/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = "blzhu" """ pyt ...