792. Number of Matching Subsequences
Given string
Sand 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 inwordsthat are a subsequence ofS: "a", "acd", "ace".
Note:
- All words in
wordsandSwill only consists of lowercase letters.- The length of
Swill be in the range of[1, 50000].- The length of
wordswill 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 ...
随机推荐
- c++ 自动对象
转自: https://www.cnblogs.com/geloutingyu/p/8034904.html 1.自动对象默认情况下,局部变量的生命期局限于所在函数的每次执行期间.只有当定义它的函数被 ...
- centos / debian 安装iptables服务
debian: #使用可参考 https://www.debian.cn/archives/991 #配置文件位于 /etc/iptables #重新配置使用dpkg-reconfigure ipta ...
- linux(rhel) rescue修复详解
修复linux,先准备好一张安装光盘,光驱安装好后开机,选择从光驱启动.等待安装盘显示操作界面时选择"rescue"选项,如果有光标提示的话,也可以输入:linux rescue进 ...
- 521. Longest Uncommon Subsequence I
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 2018.07.04 POJ 1265 Area(计算几何)
Area Time Limit: 1000MS Memory Limit: 10000K Description Being well known for its highly innovative ...
- Django入门与实践-第19章:主题回复(完结)
http://127.0.0.1:8000/boards/1/topics/1/reply/ http://127.0.0.1:8000/boards/1/topics/1/ #myproject/u ...
- linux下mysql命令大全
1.linux下启动mysql的命令: mysqladmin start /ect/init.d/mysql start (前面为mysql的安装路径) 2.linux下重启mysql的命令: mys ...
- 函数作用域和块级作用域--你不知道的JavaScript
et和const在{}内声明都会变为外部不能访问的值,但是const声明的是常量,也不能修改 函数是 JavaScript 中最常见的作用域单元.本质上,声明在一个函数内部的变量或函数会在所处的作用域 ...
- IP之ALTDDIO_in仿真
需要添加altera_mf库,才可以仿真. 上升沿输出,把前一个时钟的数据输出来. `timescale 1 ns/ 1 ns; module altddio_in_ip_tb; reg rst; r ...
- html5获取经纬度
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...