1 Star 0 Fork 0

唐梓迅/leetcode题解

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
.LeetCode 792 759 Bytes
一键复制 编辑 原始数据 按行查看 历史
唐梓迅 提交于 2022-11-17 23:30 +08:00 . 匹配子序列的单词数
class Solution {
public:
int numMatchingSubseq(string s, vector<string>& words) {
vector<vector<int>> pos(26);
for (int i = 0; i < s.size(); ++i) {
pos[s[i] - 'a'].push_back(i);
}
int res = words.size();
for (auto &w : words) {
if (w.size() > s.size()) {
--res;
continue;
}
int p = -1;
for (char c : w) {
auto &ps = pos[c - 'a'];
auto it = upper_bound(ps.begin(), ps.end(), p);
if (it == ps.end()) {
--res;
break;
}
p = *it;
}
}
return res;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/Tang-CMer/leetcode-problem-solving.git
git@gitee.com:Tang-CMer/leetcode-problem-solving.git
Tang-CMer
leetcode-problem-solving
leetcode题解
master

搜索帮助