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 ...
随机推荐
- Spring实战Day3
通过XML创建装配bean 1.装配不存在成员变量的bean <bean id="talent" class="cn.jqzhong.Spring.study.da ...
- golang实现dns域名解析(一)
本文将详细讲解如何用go语言一步一步实现dns域名解析的过程,并简单介绍点dns有关的知识,直接开始正题吧. 首先我们要了解dns解析的过程,没有了解的请看这里DNS入门(转)很详细.扫盲结束后,我们 ...
- android 获取GPS定位
AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...
- Android 自定义录音、播放动画View,让你的录音浪起来
最近公司项目有一个录音的录制和播放动画需求,然后时间是那么紧,那么赶紧开撸. 先看效果图 嗯,然后大致就是这样,按住录音,然后有一个倒计时,最外层一个进度条,还有一个类似模拟声波的动画效果(其实中间的 ...
- [zlib]_[0基础]_[使用Zlib完整解压zip内容]
场景: 1. 解压文件一般用在下载了一个zip文件之后解压,或者分析某个文件须要解压的操作上. 2. 解压文件,特别是解压带目录的zip文件往往系统没有提供这类Win32 API,当然C#自带库能解压 ...
- C#开发ActiveX控件,.NET开发OCX控件案例 【转】
http://xiaochen.2003.4.blog.163.com/blog/static/480409672012530227678/ 讲下什么是ActiveX控件,到底有什么作用?在网页中又如 ...
- odoo生产物流
odoo生产从raw materials location 自动消耗物料,产成品进入到finish productslocation. 而odoo自动产生的MO[manufacture Order], ...
- BZOJ 3727 PA2014 Final Zadanie 树形DP
题目大意:给定一棵树,令一个点到全部点的距离与点权的乘积之和为b[i].求每一个点的权值a[i] 首先假设给定a[i]我们能够非常轻松的求出b[i] 可是反过来怎么搞?高斯消元?30W? 考虑已知a[ ...
- 获取css信息
一般情况是用style直接获取css信息但是style只能获取到卸载行内的样式外链的和嵌入的样式会获取不到 2.5 用下面方法获取外链和嵌入的css样式 这种方法只能用于读取 window.getCo ...
- 前言(CSDN也有Markdown了,好开森)
实战出精华 在具体的C++网络编程中提升你的逼格 John Torjo Boost.Asio C++ 网络编程 Copyright © 2013 Packt Publishing 关于作者 做为一名权 ...