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 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].
思路:先用vector记录每个字母出现的位置,然后遍历每个单词,如果每个字母的位置是递增的那么这个单词就符合要求。如何判断位置见的递增关系呢?可以用二分实现。具体看代码
class Solution {
public:
int numMatchingSubseq(string S, vector<string>& words) {
vector<int> v[26];
for (int i = 0; i < S.size(); ++i) {
v[S[i]-'a'].emplace_back(i);
}
int ans = 0;
for (auto i : words) {
int mark = 0;
int y = -1;
for(auto c : i) {
int x = c - 'a';
if (v[x].empty()) {
mark = 1; break;
}
int pos = upper_bound(v[x].begin(), v[x].end(), y) - v[x].begin();
if (pos < v[x].size()) {
y = v[x][pos];
} else {
mark = 1; break;
}
}
if (!mark) ans++;
}
return ans;
}
};
leetcode 792. Number of Matching Subsequences的更多相关文章
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 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. 判断子序列
- [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 ...
- [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之“动态规划”:Distinct Subsequences
题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A s ...
- LeetCode(115) Distinct Subsequences
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- C#版 - Leetcode 191. Number of 1 Bits-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
随机推荐
- AC日记——最大子树和 洛谷 P1122
题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿时想到了一个有关修剪花卉的问题.于是当日课后,小明 ...
- Java 界面编程【03】事件监听
当你把界面都设计好了,总需要添加相应的执行动作给组件,在JAVA中有相应的时间处理机制,叫做“监听器”,给组件添加相应执行动作的过程叫做“注册”,其中的“监听器”是一个接口,里面包含了相应的执行函数, ...
- Java 网络通信【01】TCP
不积跬步,无以至千里:不积小流,无以成江海.——<荀子劝学> JAVA中设计网络编程模式的主要有TCP和UDP两种. TCP是属于即时通信,点对点连接进行通信. UDP是通过数据包来进行通 ...
- UVA 10635 Prince and Princess【LCS 问题转换为 LIS】
题目链接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19051 题意: 有两个长度分别为p+1和q+1的由1到n2 ...
- Linux VFS
翻译自Linux文档中的vfs.txt 介绍 VFS(Virtual File System)是内核提供的文件系统抽象层,其提供了文件系统的操作接口,可以隐藏底层不同文件系统的实现. Directir ...
- Codechef FNCS Chef and Churu
Disciption Chef has recently learnt Function and Addition. He is too exited to teach this to his fri ...
- 【mybatis】mybatis中insert操作,返回自增id
需求是这样的: mybatis中insert操作,返回自增id,因为这个自增id需要给后续业务用到. 原本是这样的: 将insert语句传入,正常执行insert操作,返回int永远是 0[失败] 或 ...
- jmeter如何在写入jtl文件时同步写入数据库
参考:1.http://blog.csdn.net/cakushin7433/article/details/53367508 2.http://blog.csdn.net/cakushin74 ...
- mac 下 virtualbox 配置全网通
mac下virtualbox实现主机和虚拟机.虚拟机和外网互访的方案 全局添加Host-Only网络 Adapter IPv4 Address:192.168.56.1 IPv4 Network Ma ...
- JavaScript-4.7-friendly_table---ShinePans
<html> <head> <meta http-equiv="content-type" content="text/html;chars ...