leetcode 792. Number of Matching Subsequences(匹配的子串数量)

从Canny到深度学习:边缘检测指标进化史(含ODS/OIS/AP的数学推导) 本文系统梳理了边缘检测评估指标的演进历程,从经典的Canny算子到深度学习时代,重点解析了ODS、OIS和AP三大核心指标的设计哲学与数学推导。文章深入探讨了基于PR曲线的评估体系,揭示了不同指标(如ODS与OIS)在阈值优化上的差异,并分析了AP指标在边缘检测任务中的优势与局限,为算法研究和模型评估提供了实用指南。 阅读详情

Given a string s and an array of strings words, return the number of words[i] that is a subsequence of s.

A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.

For example, “ace” is a subsequence of “abcde”.

Example 1:

Input: s = “abcde”, words = [“a”,“bb”,“acd”,“ace”]
Output: 3
Explanation: There are three strings in words that are a subsequence of s: “a”, “acd”, “ace”.
Example 2:

Input: s = “dsahjpjauf”, words = [“ahjpjau”,“ja”,“ahbwzgqnuk”,“tnmlanowax”]
Output: 2

判断words里面有几个是s的子串。
子串指字母都在s里,且保持相对顺序,在s中不一定是连续的。

思路:

1.binary search
可以直接用Brute force,每个word都遍历一遍s,判断是不是子串,
但这种方法在大量重复word的时候会TLE,可用一个hashmap保存下每个word判断的结果。

但是每次遍历s用的都是线性时间复杂度,用binary search的话可以把这种判断子串的时间缩短到O(logn)

具体怎么做,首先把s中的字母出现的位置都保存在list里,因为字母只有26个,所以用下标i对应第 i 个字母,
字母对应的位置保存list, 即各字母都在什么位置出现。

然后遍历word的时候,只需要顺次找到字母出现的位置,
找到前一个字母出现的位置之后,记下下标left, 下一次在left+1往后的位置找下一个字母,找不到就返回false.

同时,用2个hashset记录下已经判断过是子串和不是子串的word.

class Solution {
    List<List<Integer>> pos;
    HashSet<String> set = new HashSet<>();
    HashSet<String> not = new HashSet<>();
    
    public int numMatchingSubseq(String s, String[] words) {
        int res = 0;
        pos = new ArrayList<>();
        for(int i = 0; i < 26; i ++) {
            pos.add(new ArrayList<Integer>());
        }
        
        //保存每个字母出现的位置
        for(int i = 0; i < s.length(); i++) {
            pos.get(s.charAt(i) - 'a').add(i);
        }
        
        for(String word : words) {
            if(isMatch(word)) {
                set.add(word);
                res ++;
            } else {
                not.add(word);
            }     
        }
        return res;
    }
    
    boolean isMatch(String word) {
        if(set.contains(word)) return true;
        if(not.contains(word)) return false;
        int left = -1;
        
        for(char ch : word.toCharArray()) {
            List<Integer> curPos = pos.get(ch - 'a');
            //找left+1,即下一index,可能会找不到,但是我们要的不是确切的left+1,而是比它大的即可,
            //所以要找到需要插入的位置,即返回的负的index
            int index = Collections.binarySearch(curPos, left+1);
            if(index < 0) index = -index -1;
            if(index >= curPos.size()) return false;
            left = curPos.get(index);
        }
        return true;
    }
}

2.queue

上面方法一是对每个单词,都要遍历一次s来进行匹配,
那么有没有只遍历一次s,同时达到匹配所有单词的效果?

如果想达到这种效果,第一直觉应该是每遍历到s的一个字母,我们都要去匹配每个单词,
单词中如果有字母和s的这个字母匹配,就把单词的遍历下标+1,
那么每个单词都应该有一个遍历的下标,这么多下标要怎么维护呢?

定义一个Item, 里面是单词和目前遍历到的下标。

首先把words做一个处理,把words中每个单词首字母对应的位置加入一个queue,
这个queue保存的是现在每个单词和它们的遍历下标:0,

后面queue保存的是和当前s遍历到的字母 匹配的所有item(单词和它们相应的字母下标)。

然后遍历s中的字母,每个字母取出对应的queue, queue里面有所有以这个字母开头的单词,
字母匹配之后,把对应单词的当前下标+1,也就是下一次将匹配的字母下标,
如果下标和单词长度一致,认为已经遍历完,该单词是子串,结果+1.

class Solution {
    class Item {
        String word;
        int index;
        public Item (String s, int i) {
            word = s;
            index = i;
        }
    }
    public int numMatchingSubseq(String S, String[] words) {
        Queue<Item>[] dict = new Queue[26];
        for (int i = 0; i < dict.length; i++) {
            dict[i] = new LinkedList<>();
        }

        for (String word :words) {
            if (word.length() > 0) {
                dict[word.charAt(0) - 'a'].add(new Item(word, 0));
            }
        }

        int count = 0;
        for (char c : S.toCharArray()) {
            Queue<Item> queue = dict[c - 'a'];
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                Item top = queue.remove();
                top.index++;
                if (top.index == top.word.length()) {
                    count++;
                } else {
                    dict[top.word.charAt(top.index) - 'a'].add(top);
                }
            }
        }

        return count;
    }
}
嵌入式工程师成长之路(1)——元件基础(完整版) 为什么你的电路总是“玄学”故障?答案藏在这些元件里! 0欧电阻竟能引发信号反射?5V转3.3V电平匹配失败导致MCU烧毁?电容降压设计让电源“滋滋”响?——这些坑,本文全填平! 优快云硬件领域头部博主十年踩坑经验汇总,覆盖电阻、电容、电感核心难点+高频实战问题,让你从“调试地狱”直通“一次成功”! 阅读详情

相关推荐

告别SD卡!手把手教你用NVMe硬盘盒给Jetson Orin Nano离线烧录Ubuntu系统

本文详细介绍了如何利用NVMe硬盘盒为Jetson Orin Nano离线烧录Ubuntu系统,替代传统SD卡方案。通过硬件选择、系统镜像准备到烧录命令的全流程解析,帮助开发者提升存储性能与系统稳定性,特别适合嵌入式开发场景。

weixin_30808693的博客 459

使用kmp算法计算模式串在主串中有多少个完全匹配的子串

在做这道题时,设置一个count变量,通过if语句进行判断模式串是否已经完全匹配一次。 int count_kmp(char *s, char *p,int next[]){ int count = 0,i = 0; int j = 0; int sLen = strlen(s); int pLen = strlen(p); while(i<sLen) //因为是在主串中匹配个数,所以不能用模式串的长度作为判断循环的条件 { if (j == -1 || s[i] == p[j])

lookmeli的博客 2384

如何安装PyFluent

PyFluent 是PyAnsys生态系统的一部分, 允许您在所选的 Python 环境中结合使用 Fluent 与其他 PyAnsys 库和外部 Python 库一起使用。PyFluent 实现了客户端-服务器体系结构。它使用谷歌遥控器 过程调用或 gRPC 接口,用于启动或连接正在运行的 Fluent 进程作为服务器。但是,您只需要与 Python 交互接口。可以使用 PyFluent 以编程方式创建、交互和控制 Fluent 会话,用于创建您自己的自定义工作区。

B_etter_Man的博客 2933

(七)数据结构-串

串(string)是由零个或多个字符组成的有序序列,一般记为S=‘a1a2a3…an’(n>=10)其中S是串名,单括号括起来的字符序列是串的值;ai可以是字母、数字或者其他字符;串中字符的个数n称为串的长度;当n=0时的串称为空串;......

weixin_45579930的博客 841

拌合楼管理软件开发(十一) 海康威视车牌识别摄像头安装调试,总算是跑通了。

/保存的文件个数这里之所以需要用到委托,直接更新textbox文本中的内容,以及复制picturebox的image会提示不是从齐创建的线程调用。

morsth的博客 4824

SRC漏洞挖掘实战指南:从零入门到月入过万的网络安全副业

网络安全的核心在于识别和修复系统缺陷,其中逻辑漏洞与信息泄露是常见的安全风险。其原理在于应用程序在业务逻辑设计或数据处理过程中存在缺陷,导致未授权访问或敏感数据暴露。掌握这些技术对于构建安全的应用体系至关重要,不仅能有效防范攻击,也为安全研究人员提供了将技术能力变现的合法途径。在漏洞赏金(Bug Bounty)和SRC(安全应急响应中心)等应用场景中,白帽子通过系统化的测试方法,挖掘此类漏洞,既能获得经济回报,又能积累宝贵的实战经验。本文聚焦于逻辑漏洞与信息泄露的实战挖掘技巧,为初学者提供清晰的入门路径和效

weixin_28568819的博客 351

排序算法十:桶排序

排序算法十:桶排序 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 引言 在我的博文《“主宰世界”的10种算法短评》中给出的首个算法就是高效的排序算法。本文将对排序算法做一个全面的梳理,从最简单的“冒泡”到高效的堆排序等。 系列博文的上一篇讲述了基数排序,本文讲...

weixin_30530339的博客 374

stm32开发3D打印机(一)——介绍(已完成)

如何运用stm32开发3D打印机

Nico_jion的博客 1万+

【leetcode】792. Number of Matching Subsequences

给定字符串S和一个字符串数组,求数组中有多少个字符串是S的子字符串,子字符串的定义是可以母串可以通过删减元素成为子串。 方法一: 首先想到的就是双指针法,对字符串中的每个元素进行匹配操作,来判断是否为字字符串,想想都知道会超时的。 class Solution: def numMatchingSubseq(self, S, words): """ :typ...

很吵请安静 587

麒麟信安3.3-6C电网版虚拟机安装避坑指南:从ISO下载到SSH配置全流程

本文提供麒麟信安3.3-6C电网版虚拟机的详细安装与配置指南。针对电力行业仿真测试需求,重点解析从ISO镜像获取、虚拟机环境准备、安装过程中的三大“雷区”(如多管理员密码逻辑、分区方案、网络配置)到首次启动后的网络、SSH及root策略配置的全流程避坑要点,帮助技术人员高效、安全地完成部署。

rainy的博客 856

基于 Arduino 的鱼缸水质检测控制系统设计

本系统通过多传感器实时采集鱼缸水质参数(pH 值、温度、溶解氧、浊度),经 Arduino 主控芯片处理后,驱动执行模块自动调节水质,并通过显示模块实时展示数据。系统具备阈值报警、自动换水、温度调节、增氧等功能,适用于家庭或小型水族箱的水质智能管理。本设计提供了低成本、易扩展的鱼缸水质管理方案,适用于家庭水族箱或小型水产养殖场景,可根据实际需求调整传感器类型和控制逻辑。注:本设计如有疑问需要解惑可直接私信本人,看我个人简介。

weixin_42300449的博客 1088

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

【LeetCode】792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/number-of-matching-subsequences/description...

负雪明烛 2439

秋招突击——笔试整理——8/25——拼多多笔试整理

继续加油! 转变思维,有简单到复杂,由直观到抽象,这种贪心模拟的题目一直是这样的!

Blackoutdragon的博客 2404

leetcode 792. Number of Matching Subsequences

leetcode 792. Number of Matching Subsequences leetcode 792. Number of Matching Subsequences 题目描述 解答思路 代码 题目描述 Given string S and a dictionary of words words, find the number of words[i] t...

pjq的博客 1325

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

[leetcode] 792. Number of Matching Subsequences

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

Learning from the mistakes 284

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

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

`words`中是`S`的子串的数量

grllery的博客 457

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

【天梯】子字符串个数匹配 (15 分)

#include<bits/stdc++.h> using namespace std; int main() { string a,b; getline(cin,a); cin>>b; for(int i=0;a[i];i++) { if(a[i]>='A'&&a[i]<='Z') a[i]=a[i]+32; if(!(a[i]>='a'&&a[i]<='z')) a[i]=' '; } ..

karshey的博客 776

792. Number of Matching Subsequences

最近开始同时使用python和go做题,go的效率高很多,用python brute force的方法提交以后TLE,同样的方法用go可以AC,用python可以榨取更快的算法。 题目 给定一个字符串S和一个单词列表words ,找到words里所有S的子串。 Example : Input: S = “abcde” words = [“a”, “bb”, “acd”, “ace”] Outpu...

qq_35753140的博客 293

[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 Explanatio

魔豆(Magicbean)的博客 522

[LeetCode] Distinct Subsequences

class Solution { public: int numDistinct(string S, string T) { // Start typing your C/C++ solution below // DO NOT write int main() function int m = T.size(); int n

lyangpku的专栏 701

LWC 74: 792. Number of Matching Subsequences

LWC 74: 792. Number of Matching Subsequences 传送门:792. Number of Matching Subsequences Problem: Given string S and a dictionary of words words, find the number of words[i] that is a subsequence o...

Demon-初来驾到 1425

795. Number of Subarrays with Bounded Maximum

  We are given an array A of positive integers, and two positive integers L and R (L &lt;= R). Return the number of (contiguous, non-empty) subarrays such that the value of the maximum array element...

Do not mess it up 738

[LeetCode 792] Number of Matching Subsequences

Given stringSand adictionary of wordswords, find the number ofwords[i]that is a subsequence ofS. Example : Input: S = "abcde" words = ["a", "bb", "acd", "ace"] Output: 3 Explanation: There ...

ExcitedZhang的博客 194

Number of Matching Subsequences--leetcode 792

[Number of Matching Subsequences]  792-问题描述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", ...

drunkpragrammer的博客 887

LeetCode_115---Distinct Subsequences

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none)

MohnSnow,Winter is Coming!!! 495

leetcode#792. Number of Matching Subsequences

792. Number of Matching Subsequences Problem Description Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of S. Analysis and Solution 基本思路是对wor...

Faldict的博客 740
上一篇: RGB+深度图像 语义分割paper阅读笔记(ICRA2021)
下一篇: leetcode 92. Reverse Linked List II(链表逆序II)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值