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

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的更多相关文章

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

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

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

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

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

  4. [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 ...

  5. [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 ...

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

  7. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  8. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. Castle ActiveRecord学习(三)数据映射及特性描述

    Model中的Demo: using Castle.ActiveRecord; using Castle.ActiveRecord.Queries; using System; using Syste ...

  2. sql ltrim/rtrim 字段中为中文时出现?的问题

    字段存储为中文,类型为nvarchar,使用ltrim时结果集中出现的问号,我的解决办法是:将问号replace掉

  3. Vim基础教程

    一.简介 世界上只有三种编辑器,EMACS.VIM和其它. 我们所处的时代是非常幸运的,有越来越多的编辑器,相对于古老的VIM和EMACS,它们被称为现代编辑器.我们来看看这两个古董有多大年纪了: * ...

  4. DNA拷贝数变异CNV检测——基础概念篇

    DNA拷贝数变异CNV检测——基础概念篇   一.CNV 简介 拷贝数异常(copy number variations, CNVs)是属于基因组结构变异(structural variation), ...

  5. 建表Table

    Sstudent表   学  号    Sno  姓  名   Sname   性  别    Ssex     年  龄      Sage   所 在 系    Sdept   200215121 ...

  6. Spring官方文档翻译(1~6章)

    Spring官方文档翻译(1~6章) 转载至 http://blog.csdn.net/tangtong1/article/details/51326887 Spring官方文档.参考中文文档 一.S ...

  7. VS2010 MFC 使用GDI+给图片添加汉字

    1.配置GDI+ VS2010自带GDI+,直接使用. (1)首先要添加头文件和库 #pragma comment( lib, "gdiplus.lib" ) #include & ...

  8. hadoop学习笔记(四):hdfs常用命令

    一.hadoop fs 1.创建目录 [root@master hadoop-]# hadoop fs -mkdir /testdir1 [root@master hadoop-]# hadoop f ...

  9. python面向对象-3类的静态方法和类方法

    还是以上次的洗衣机例子: class Washer: company='ZBL' def __init__(self,water=10,scour=2): self._water=water #不想让 ...

  10. JavaNIO学习一

    文章来源:http://cucaracha.iteye.com/blog/2041847 对文件来说,可能最常用的操作就是创建.读取和写出.NIO.2 提供了丰富的方法来完成这些任务.本文从简单的小文 ...