[LeetCode44]Wildcard Matching

2023-11-01 15:33

本文主要是介绍[LeetCode44]Wildcard Matching,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Implement wildcard pattern matching with support for '?' and '*'.

'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).The matching should cover the entire input string (not partial).The function prototype should be:
bool isMatch(const char *s, const char *p)Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false
Analysis:

Greedy method

For each element in s
If *s==*p or *p == ? which means this is a match, then goes to next element s++ p++.
If p=='*', this is also a match, but one or many chars may be available, so let us save this *'s position and the matched s position.
If not match, then we check if there is a * previously showed up,
if there is no *, return false;
if there is an *, we set current p to the next element of *, and set current s to the next saved s position.

e.g.

abed
?b*d**

a=?, go on, b=b, go on,
e=*, save * position star=3, save s position ss = 3, p++
e!=d, check if there was a *, yes, ss++, s=ss; p=star+1
d=d, go on, meet the end.
check the rest element in p, if all are *, true, else false;

Note that in char array, the last is NOT NULL, to check the end, use "*p" or "*p=='\0'".

Java

public boolean isMatch(String s, String p) {int slen = s.length();int plen = p.length();int i = 0, j=0;int star = -1;int sp = 0;while(i<slen){while(j<plen && p.charAt(j)=='*'){star = j++;sp = i;}if(j==plen || (s.charAt(i)!=p.charAt(j)&&p.charAt(j)!='?')){if(star<0) return false;else {j = star+1;i = sp++;}}else {i++;j++;}}while(j<plen && p.charAt(j)=='*'){j++;}return j == plen;}

c++

bool isMatch(const char *s, const char *p) {const char* star = NULL;const char* ss = s;while(*s){if((*p=='?') || *p==*s){s++;p++;continue;}if(*p=='*'){star = p++;ss = s;continue;}if(star){p = star+1;s=++ss;continue;}return false;}while(*p=='*'){p++;}return !*p;}



这篇关于[LeetCode44]Wildcard Matching的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/323995

相关文章

leetcode#10. Regular Expression Matching

题目 Implement regular expression matching with support for ‘.’ and ‘*’. '.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input

ssh登录服务器报错“no matching host key type found. Their offer: ssh-rsa,ssh-dss”解决方法

这个错误表明你尝试使用 ssh 连接到远程服务器时,客户端和服务器之间没有匹配的 host key 类型。具体来说,远程服务器提供了 ssh-rsa 和 ssh-dss 类型的 host key,但你的 SSH 客户端配置可能不再支持这些较旧的算法。最近的 OpenSSH 版本默认禁用了不够安全的算法,如 ssh-rsa 和 ssh-dss。 解决方法 临时启用 ssh-rsa: 你可以在

One-Shot Imitation Learning with Invariance Matching for Robotic Manipulation

发表时间:5 Jun 2024 论文链接:https://readpaper.com/pdf-annotate/note?pdfId=2408639872513958656&noteId=2408640378699078912 作者单位:Rutgers University Motivation:学习一个通用的policy,可以执行一组不同的操作任务,是机器人技术中一个有前途的新方向。然而,

LeetCode - 10. Regular Expression Matching

10. Regular Expression Matching Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个串s和一个自动机p(模糊字符只含有'.'和'*'),问串s是否能够和自动机p匹配. analyse:

NLP-文本匹配-2017:BiMPM【Bilateral Multi-Perspective Matching for Natural Language Sentences】

NLP-文本匹配-2016:BiMPM【Bilateral Multi-Perspective Matching for Natural Language Sentences】

NLP-文本匹配-2016:Compare-Aggregate【A Compare-Aggregate Model for Matching Text Sequences】

NLP-文本匹配-2016:Compare-Aggregate【A Compare-Aggregate Model for Matching Text Sequences】

PeriodWave: Multi-Period Flow Matching for High-Fidelity Waveform Generation

preprintKorea Seoul, Korea 文章目录 abstractmethodFlow Matching for Waveform GenerationHigh-frequency Information Modeling for Flow Matching demo page, PeriodWave 三者最好,而且能把原声中的噪声去掉,GAN一类声码器做不到的。 Perio

PSMNet:Pyramid Stereo Matching Network学习测试笔记04-特征提取部分前向传播

写在前面的话: 2019年9月21日18:56:48好久没回来更新博客了。因为在实习中,实习的新问题一大堆,并且实习的工作内容整理了也是发在公司内网wiki,外面是不可能发的(专业,有节操)。周末再做做毕业论文相关的工作。 写在前面的话2: 2019年09月28日18:02:55补充说明:CSDN博客发布版权更新,如果您看了博客并且用到PSMNet相关东西,请注明引用原作者的文章: @inpr

PSMNet:Pyramid Stereo Matching Network学习测试笔记03-如何训练网络

写在前面的话: 2019年09月28日18:02:55补充说明:CSDN博客发布版权更新,如果您看了博客并且用到PSMNet相关东西,请注明引用原作者的文章: @inproceedings{chang2018pyramid, title={Pyramid Stereo Matching Network}, author={Chang, Jia-Ren and Chen, Yong-Sheng}

MKS MWH-5匹配器Automatc matching impedance Network手侧

MKS MWH-5匹配器Automatc matching impedance Network手侧