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,又给了一个单词数组,问我们数组有多少个单词是字符串S的子序列。注意这里是子序列,而不是子串,子序列并不需要连续。那么只要我们知道如何验证一个子序列的方法,那么就可以先尝试暴力搜索法,就是对数组中的每个单词都验证一下是否是字符串S的子序列。验证子序列的方法就是用两个指针,对于子序列的每个一个字符,都需要在母字符中找到相同的,在母字符串所有字符串遍历完之后或之前,只要子序列中的每个字符都在母字符串中按顺序找到了,那么就验证成功了。很不幸,这种暴力搜索的方法在C++的解法版本中会TLE,貌似Java版本的可以通过,感觉C++被dis了诶~ However,我们可以进行优化呀,在暴力搜索的基础上稍作些优化,就可以骗过OJ啦。下面这种优化的motivation是由于看了使暴力搜索跪了的那个test case,其实是words数组里有大量相同的单词,而且字符串S巨长无比,那么为了避免相同的单词被不停的重复检验,我们用两个HashSet来记录验证过的单词,为啥要用两个呢?因为验证的结果有两种,要么通过,要么失败,我们要分别存在两个HashSet中,这样再遇到每种情况的单词时,我们就知道要不要结果增1了。如果单词没有验证过的话,那么我们就用双指针的方法进行验证,然后根据结果的不同,存到相应的HashSet中去,参见代码如下:

解法一:

class Solution {
public:
int numMatchingSubseq(string S, vector<string>& words) {
int res = , n = S.size();
unordered_set<string> pass, out;
for (string word : words) {
if (pass.count(word) || out.count(word)) {
if (pass.count(word)) ++res;
continue;
}
int i = , j = , m = word.size();
while (i < n && j < m) {
if (word[j] == S[i]) ++j;
++i;
}
if (j == m) {++res; pass.insert(word);}
else out.insert(word);
}
return res;
}
};

上面的解法已经优化的不错了,但是我们还有更叼的方法。这种解法按照每个单词的首字符进行群组,群组里面保存的是一个pair对,由当前字母和下一个位置组成的。然后在遍历字符串S的时候,根据当前遍历到的字母,进入该字母对应的群组中处理,如果群组中某个pair的下一个位置已经等于单词长度了,说明该单词已经验证完成,是子序列,结果自增1;否则的话就将下一个位置的字母提取出来,然后将pair中的下一个位置自增1后组成的新pair加入之前提取出的字母对应的群组中。是不是读到这里已然懵逼了,没关系,博主会举栗子来说明的,就拿题目中的那个例子来说吧:

S = "abcde"

words = ["a", "bb", "acd", "ace"]

那么首先我们将words数组中的单词按照其首字母的不同放入对应的群组中,得到:

a -> {0, 1}, {2, 1}, {3, 1}

b -> {1, 1}

这里,每个pair的第一个数字是该单词在words中的位置,第二个数字是下一个字母的位置。比如 {0, 1} 表示 "a" 在words数组中位置为0,且下一个位置为1(因为当前位置是首字母)。{2, 1} 表示 "acd" 在words数组中位置为2,且下一个位置为1。{3, 1} 表示 "ace" 在words数组中位置为3,且下一个位置为1。{1, 1} 表示 "bb" 在words数组中位置为1,且下一个位置为1。

好,下面我们来遍历字符串S,第一个遇到的字母是 'a'。

那么我们群组中a对应了三个pair,将其提取出来分别进行操作。首先处理 {0, 1},此时我们发现下一个位置为1,和单词"a"的长度相同了,说明是子序列,结果res自增1。然后处理 {2, 1},在"acd"中取下一个位置1的字母为'c',则将下一位置自增1后的新pair {2, 2} 加入c对应的群组。然后处理 {3, 1},在"ace"中取下一个位置1的字母为'c',则将下一位置自增1后的新pair {3, 2} 加入c对应的群组。则此时的群组为:

b -> {1, 1}

c -> {2, 2}, {3, 2}

好,继续来遍历字符串S,第二个遇到的字母是 'b'。

那么我们群组中b对应了一个pair,处理 {1, 1},在"bb"中取下一个位置1的字母为'b',则将下一位置自增1后的新pair {1, 2} 加入b对应的群组。则此时的群组为:

b -> {1, 2}

c -> {2, 2}, {3, 2}

好,继续来遍历字符串S,第三个遇到的字母是 'c'。

那么我们群组中c对应了两个pair,将其提取出来分别进行操作。首先处理 {2, 2},在"ace"中取下一个位置2的字母为'e',则将下一位置自增1后的新pair {2, 3} 加入e对应的群组。然后处理 {3, 2},在"acd"中取下一个位置2的字母为'd',则将下一位置自增1后的新pair {3, 3} 加入d对应的群组。则此时的群组为:

b -> {1, 2}

d -> {3, 3}

e -> {2, 3}

好,继续来遍历字符串S,第四个遇到的字母是 'd'。

那么我们群组中d对应了一个pair,处理 {3, 3},此时我们发现下一个位置为3,和单词"acd"的长度相同了,说明是子序列,结果res自增1。则此时的群组为:

b -> {1, 2}

e -> {2, 3}

好,继续来遍历字符串S,第五个遇到的字母是 'e'。

那么我们群组中e对应了一个pair,处理 {2, 3},此时我们发现下一个位置为3,和单词"ace"的长度相同了,说明是子序列,结果res自增1。则此时的群组为:

b -> {1, 2}

此时S已经遍历完了,已经没有b了,说明"bb"不是子序列,这make sense,返回结果res即可,参见代码如下:

解法二:

class Solution {
public:
int numMatchingSubseq(string S, vector<string>& words) {
vector<pair<int, int>> all[];
int res = , n = words.size();
for (int i = ; i < n; i++) {
all[words[i][]].emplace_back(i, );
}
for (char c : S) {
auto vect = all[c];
all[c].clear();
for (auto it : vect) {
if (it.second == words[it.first].size()) ++res;
else all[words[it.first][it.second++]].push_back(it);
}
}
return res;
}
};

类似题目:

Is Subsequence

参考资料:

https://leetcode.com/problems/number-of-matching-subsequences/solution/

https://leetcode.com/problems/number-of-matching-subsequences/discuss/117634/Efficient-and-simple-go-through-words-in-parallel-with-explanation

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

[LeetCode] Number of Matching Subsequences 匹配的子序列的个数的更多相关文章

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

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

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

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

  3. [LeetCode] 44. Wildcard Matching 外卡匹配

    Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...

  4. Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划

    Given a string S and a string T, count the number of distinct subsequences ofT inS. A subsequence of ...

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

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

  8. [LeetCode] Regular Expression Matching 正则表达式匹配

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

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

随机推荐

  1. Java WebService接口生成和调用 图文详解>【转】【待调整】

    webservice简介: Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的.专门的第三方软件或硬件, 就可相互交换数据或集成.依据Web Service规范实施的应用之间 ...

  2. css长度单位学习(em,rem,px,vw,vh)

    绝对长度单位 绝对长度单位代表一个物理测量 [像素px(pixels)] 像素,为影像显示的基本单位,译自英文"pixel",pix是英语单词picture的常用简写,加上英语单词 ...

  3. [物理学与PDEs]第1章习题1 无限长直线的电场强度与电势

    设有一均匀分布着电荷的无限长直线, 其上的电荷线密度 (即单位长度上的电荷量) 为 $\sigma$. 试求该直线所形成的电场的电场强度及电势. 解答: 设空间上点 $P$ 到直线的距离为 $r$, ...

  4. gitlab升级迁移(二)

    前面我们写了一篇gitlab升级迁移的文章(https://www.cnblogs.com/liangyou666/p/9434158.html),这次我们主要是讲另一种升级迁移方法和其中遇到的一些问 ...

  5. [C++]数据结构-排序:插入排序之直接插入排序

    得赶紧休息了,木有时间写原理了.直接上代码. /* <插入排序-直接插入排序> */ #include<iostream> using namespace std; void ...

  6. ZOC7在Mac下发送命令到多个窗口设置

    1 详见截图,找了半天 2 然后,下边框就会出现命令发送多个窗口的输入框了

  7. 求逆序对常用的两种算法 ----归并排 & 树状数组

    网上看了一些归并排求逆序对的文章,又看了一些树状数组的,觉得自己也写一篇试试看吧,然后本文大体也就讲个思路(没有例题),但是还是会有个程序框架的 好了下面是正文 归并排求逆序对 树状数组求逆序对 一. ...

  8. linux中epoll模型

    epoll是linux内核为处理大批量文件描述符而作了改进的poll,是linux下IO复用select/poll的增强版本. 一.epoll的主要接口是: 1.创建 (1)int epoll_cre ...

  9. jersey+maven构建restful服务

    一.新建一个Maven Web项目 a) 新建一个简单的Maven项目 b) 将简单的Maven项目转成Web项目 (若没出现further configuration available--或里面的 ...

  10. 绑定bindchange事件的微信小程序swiper闪烁,抖动问题解决,(将微信小程序切换到后台一段时间,再打开微信小程序,会出现疯狂循环轮播,造成抖动现象)

    微信小程序开发文档-组件-swiper后面追加的新闻如上图所示: 如果在bindchange事件给swiper的current属性对应的值{{current}}赋值,就会造成抖动现象. bindcha ...