当前位置:网站首页>[LeetCode] Wildcard Matching 外卡匹配
[LeetCode] Wildcard Matching 外卡匹配
2022-07-05 11:22:00 【全栈程序员站长】
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这道题通配符匹配问题还是小有难度的,这道里用了贪婪算法Greedy Alogrithm来解,由于有特殊字符*和?,其中?能代替任何字符,*能代替任何字符串,那么我们需要定义几个额外的指针,其中scur和pcur分别指向当前遍历到的字符,再定义pstar指向p中最后一个*的位置,sstar指向此时对应的s的位置,具体算法如下:
– 定义scur, pcur, sstar, pstar
– 如果*scur存在
– 如果*scur等于*pcur或者*pcur为 ‘?’,则scur和pcur都自增1
– 如果*pcur为’*’,则pstar指向pcur位置,pcur自增1,且sstar指向scur
– 如果pstar存在,则pcur指向pstar的下一个位置,scur指向sstar自增1后的位置
– 如果pcur为’*’,则pcur自增1
– 若*pcur存在,返回False,若不存在,返回True
C 解法一:
bool isMatch(char *s, char *p) {
char *scur = s, *pcur = p, *sstar = NULL, *pstar = NULL;
while (*scur) {
if (*scur == *pcur || *pcur == '?') {
++scur;
++pcur;
} else if (*pcur == '*') {
pstar = pcur++;
sstar = scur;
} else if (pstar) {
pcur = pstar + 1;
scur = ++sstar;
} else return false;
}
while (*pcur == '*') ++pcur;
return !*pcur;
}这道题也能用动态规划Dynamic Programming来解,写法跟之前那道题Regular Expression Matching很像,但是还是不一样。外卡匹配和正则匹配最大的区别就是在星号的使用规则上,对于正则匹配来说,星号不能单独存在,前面必须要有一个字符,而星号存在的意义就是表明前面这个字符的个数可以是任意个,包括0个,那么就是说即使前面这个字符并没有在s中出现过也无所谓,只要后面的能匹配上就可以了。而外卡匹配就不是这样的,外卡匹配中的星号跟前面的字符没有半毛钱关系,如果前面的字符没有匹配上,那么直接返回false了,根本不用管星号。而星号存在的作用是可以表示任意的字符串,当然只是当匹配字符串缺少一些字符的时候起作用,当匹配字符串包含目标字符串没有的字符时,将无法成功匹配。
C++ 解法二:
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.size(), n = p.size();
vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false));
dp[0][0] = true;
for (int i = 1; i <= n; ++i) {
if (p[i - 1] == '*') dp[0][i] = dp[0][i - 1];
}
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (p[j - 1] == '*') {
dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
} else {
dp[i][j] = (s[i - 1] == p[j - 1] || p[j - 1] == '?') && dp[i - 1][j - 1];
}
}
}
return dp[m][n];
}
};类似题目:
参考资料:
https://discuss.leetcode.com/topic/3040/linear-runtime-and-constant-space-solution
https://discuss.leetcode.com/topic/40118/clear-c-dp-solution-similar-to-the-last-matching-problem
LeetCode All in One 题目讲解汇总(持续更新中…)
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/109497.html原文链接:https://javaforall.cn
边栏推荐
- Cron表达式(七子表达式)
- 2022 Pengcheng cup Web
- 【爬虫】wasm遇到的bug
- How to make full-color LED display more energy-saving and environmental protection
- Bracket matching problem (STL)
- IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
- String
- What does cross-border e-commerce mean? What do you mainly do? What are the business models?
- 【全网首发】(大表小技巧)有时候 2 小时的 SQL 操作,可能只要 1 分钟
- 爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
猜你喜欢
![[office] eight usages of if function in Excel](/img/ce/ea481ab947b25937a28ab5540ce323.png)
[office] eight usages of if function in Excel
![[advertising system] parameter server distributed training](/img/8b/558c2fefbd16b580546003f3afeaf5.png)
[advertising system] parameter server distributed training

Harbor镜像仓库搭建

DDRx寻址原理

Evolution of multi-objective sorting model for classified tab commodity flow

COMSOL--三维图形的建立

OneForAll安装使用

紫光展锐全球首个5G R17 IoT NTN卫星物联网上星实测完成
![[advertising system] incremental training & feature access / feature elimination](/img/14/ac596fa4d92e7b245e08cea014a4ab.png)
[advertising system] incremental training & feature access / feature elimination

Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
随机推荐
Cron表达式(七子表达式)
Oneforall installation and use
TSQL – identity column, guid, sequence
如何让全彩LED显示屏更加节能环保
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
Web Security
OneForAll安装使用
Deepfake tutorial
龙蜥社区第九次运营委员会会议顺利召开
Basic testing process of CSDN Software Testing Introduction
How did the situation that NFT trading market mainly uses eth standard for trading come into being?
Characteristics and electrical parameters of DDR4
解决grpc连接问题Dial成功状态为TransientFailure
An error is reported in the process of using gbase 8C database: 80000305, host IPS long to different cluster. How to solve it?
DOM//
【Oracle】使用DataGrip连接Oracle数据库
Cross page communication
Pytorch training process was interrupted
Harbor镜像仓库搭建
Lombok 同时使⽤@Data和@Builder 的坑,你中招没?