44. Wildcard Matching (String; DP, Back-Track)
Implement wildcard pattern matching with support for '?' and '*'.
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).不同于正则表达式中的*
*正则表达式的定义:
- '.' Matches any single character.
- '*' Matches zero or more of the preceding element.
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
思路I:当遇到*,有把*跳过,和继续保留*两种option=>带回溯的递归。其实也可称之为贪心法,贪心法则是每次都使*匹配尽可能少的字符。
class Solution {
public:
bool isMatch(string s, string p) {
return backTracking(s,p,,);
}
bool backTracking(string s, string p, int sp, int pp){
//end condition
if(sp==s.length()){
while(pp<p.length() &&p[pp]=='*' ){
pp++;
}
if(pp == p.length()) return true;
else return false;
}
if(pp==p.length()) return false;
if(p[pp]=='*'){
while(pp+<p.length() && p[pp+]=='*') pp++; //ignore the stars directly behind star
if(backTracking(s,p,sp,pp+)) return true; //* not repeats
return backTracking(s,p,sp+,pp); //* repeats
}
else if(s[sp]==p[pp] || p[pp]=='?') return backTracking(s,p,sp+,pp+);
else return false;
}
};
时间复杂度:二叉recursion的高度是2n 所以O(2n)
Result: Time Limit Exceeded
思路II:依然是带回溯的递归,只是记录下*号位置,和匹配的字符数,那么等到某次*不匹配时可直接回到该位置。
class Solution {
public:
bool isMatch(string s, string p) {
star = false;
return recursiveCheck(s,p,,);
}
bool recursiveCheck(const string &s, const string &p, int sIndex, int pIndex){
if(sIndex >= s.length()){
while(p[pIndex] == '*' && pIndex < p.length()) pIndex++; //s has went to end, check if the rest of p are all *
return (pIndex==p.length());
}
if(pIndex >= p.length()){
return checkStar(s,p);
}
switch(p[pIndex]) //p: pattern,在p中才可能出现?, *
{
case '?':
return recursiveCheck(s, p, sIndex+, pIndex+);
break;
case '*': //如果当前为*, 那么可认为之前的字符都匹配上了,并且将p移动到 * 结束后的第一个字符
star = true; //p 每次指向的位置,要么是最开始,要么是 * 结束的第一个位置
starIndex = pIndex;
matchedIndex = sIndex-;
while(p[pIndex] == '*'&& pIndex < p.length()){pIndex++;} //忽略紧接在 *后面的*
if(pIndex==p.length()) return true;//最后一位是*
return recursiveCheck(s,p,sIndex,pIndex); //*匹配0个字符
break;
default:
if(s[sIndex] != p[pIndex]) return checkStar(s, p);
else return recursiveCheck(s, p, sIndex+, pIndex+);
break;
}
}
bool checkStar(const string &s, const string &p){
if(!star) return false;
else {
int pIndex = starIndex+;
int sIndex = ++matchedIndex; //回溯,*d多匹配一个字符
return recursiveCheck(s, p, sIndex, pIndex);
}
}
private:
int starIndex;
int matchedIndex;
bool star;
};
Result: Approved.
思路III:使用dp。dp[i][j]表示从字符串到i位置,模式串到j位置是否匹配。
class Solution {
public:
bool isMatch(string s, string p) {
int sLen = s.length();
int pLen = p.length();
if(sLen == ){
int pp = ;
while(pp<p.length() &&p[pp]=='*' ){
pp++;
}
if(pp == p.length()) return true;
else return false;
}
if(pLen == ) return false;
int len = ;
for(int i = ;i < pLen;i++)
if(p[i] != '*') len++;
if(len > sLen) return false;
bool dp[sLen][pLen];
int i = , j = ;
for(;i<sLen;i++){
for(;j<pLen;j++){
dp[i][j]=false;
}
}
if(p[]=='*'){ //c;*?*
for(i = ;i < sLen; i++ ){
dp[i][] = true;
}
}
//first line can appear one letter which is not star
if (p[]=='?' || s[] == p[]){ //first not-star-letter appears
dp[][] = true;
for(j = ;(j < pLen && p[j]=='*'); j++ ){
dp[][j]=true;
}
}
else if(p[]=='*'){
for(j = ;(j < pLen && p[j-]=='*'); j++ ){
if(p[j]=='?' || s[] == p[j]){ //first not-star-letter appears
dp[][j]=true;
j++;
for(;j<pLen && p[j]=='*'; j++){ //after first not star, there should be all star
dp[][j]=true;
}
break;
}
else if(p[j]=='*'){
dp[][j]=true;
}
}
}
for(i = ; i < sLen; i++){
for(j = ; j < pLen; j++){
if(p[j]=='*'){
dp[i][j] = dp[i-][j] //* repeat 1 time
|| dp[i][j-]; //*repeat 0 times
}
else if(s[i]==p[j] || p[j]=='?'){
dp[i][j] = dp[i-][j-];
}
}
}
return dp[sLen-][pLen-];
}
};
时间复杂度:O(n2)
思路IV: 思路III的初始状态求法太复杂=>Solution:定义一个fake head。dp[0][0]表示两个空字符串的匹配情况,dp[0][0]=true.
class Solution {
public:
bool isMatch(string s, string p) {
int sLen = s.length();
int pLen = p.length();
if(sLen == ){
int pp = ;
while(pp<p.length() &&p[pp]=='*' ){
pp++;
}
if(pp == p.length()) return true;
else return false;
}
if(pLen == ) return false;
vector<vector<bool>> dp(sLen+, vector<bool>(pLen+,));
//initial states
int i = , j = ;
dp[][]=true;
for(j = ;(j <= pLen && p[j-]=='*'); j++ ){
dp[][j]=true;
}
//state transfer
for(i = ; i <= sLen; i++){
for(j = ; j <= pLen; j++){
if(p[j-]=='*'){
dp[i][j] = dp[i-][j] //* repeat 1 time
|| dp[i][j-]; //*repeat 0 times
}
else if(s[i-]==p[j-] || p[j-]=='?'){
dp[i][j] = dp[i-][j-];
}
}
}
return dp[sLen][pLen];
}
};
思路V:节约空间,状态之和i-1有关,所以只要记录上一行状态就可以。可以用一维数组。
class Solution {
public:
bool isMatch(string s, string p) {
int sLen = s.length();
int pLen = p.length();
if(sLen == ){
int pp = ;
while(pp<p.length() &&p[pp]=='*' ){
pp++;
}
if(pp == p.length()) return true;
else return false;
}
if(pLen == ) return false;
vector<bool> lastDP(pLen+, );
vector<bool> currentDP(pLen+, );
vector<bool> tmp;
//initial states
int i = , j = ;
lastDP[]=true;
for(j = ;(j <= pLen && p[j-]=='*'); j++ ){
lastDP[j]=true;
}
//state transfer
for(i = ; i <= sLen; i++){
currentDP[]=false;
for(j = ; j <= pLen; j++){
if(p[j-]=='*'){
currentDP[j] = lastDP[j] //* repeat 1 time
|| currentDP[j-]; //*repeat 0 times
}
else if(s[i-]==p[j-] || p[j-]=='?'){
currentDP[j] = lastDP[j-];
}
else{
currentDP[j] = false;
}
}
tmp = currentDP;
currentDP = lastDP;
lastDP = tmp;
}
return lastDP[pLen];
}
};
44. Wildcard Matching (String; DP, Back-Track)的更多相关文章
- 44. Wildcard Matching
题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- LeetCode - 44. Wildcard Matching
44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...
- 【LeetCode】44. Wildcard Matching (2 solutions)
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- [LeetCode] 44. Wildcard Matching 外卡匹配
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...
- [leetcode]44. Wildcard Matching万能符匹配
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...
- 44. Wildcard Matching 有简写的字符串匹配
[抄题]: Given an input string (s) and a pattern (p), implement wildcard pattern matching with support ...
- 【一天一道LeetCode】#44. Wildcard Matching
一天一道LeetCode系列 (一)题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches a ...
- 44. Wildcard Matching *HARD*
'?' Matches any single character. '*' Matches any sequence of characters (including the empty sequen ...
随机推荐
- PostgreSQL逻辑复制槽
Schema | Name | Result data type | Argument data types | Type ------------+------------------------- ...
- HDU2888 Check Corners(二维RMQ)
有一个矩阵,每次查询一个子矩阵,判断这个子矩阵的最大值是不是在这个子矩阵的四个角上 裸的二维RMQ #pragma comment(linker, "/STACK:1677721600&qu ...
- streamsets record header 属性
record 的header 属性可以在pipeline 逻辑中使用. 有写stages 会为了特殊目录创建reord header 属性,比如(cdc)需要进行crud 操作类型的区分 你可以使用一 ...
- prisma middleware 简化 graphql resolver 编写的类库
prisma 推出middleware 的目的就是保持resolver 的简洁 作用: 输入参数访问同一个resolver 决定resolver 最终的返回值 在resolver 连中捕获异常以及 ...
- cocos2dx 安卓真机调试问题汇总
cocos compile编译apk问题汇总: 1,dx编译报错,没有足够的空间 ANTBUILD : [dx] error : Could not create the Java Virtual M ...
- PHP 中的文本截取分析之效率
PHP 中的文本截取分析之效率 在使用 ThinkPHP 或 Laravel 时都会有用到文本截取的帮助函数. 分别是 msubstr (ThinkPHP 3,ThinkPHP 5 没找到) mb_s ...
- opensuse下配置IP、DNS、GATEWAY
本人物理主机IP描述 IPv4 地址 . . . . . . . . . . . . : 192.168.1.101(首选)子网掩码 . . . . . . . . . . . . : 255.25 ...
- java Long
1. Long.valueOf(b) 返回的是对象 public static Long valueOf(String s) throws NumberFormatException { )); } ...
- 微软开源rDSN分布式系统开发框架
摘要:微软亚洲研究院系统组开发的分布式系统开发框架——Robust Distributed System Nucleus(rDSN)正式在GitHub平台开源.据悉,rDSN是一个旨在为广大分布式系统 ...
- 视频支持拖动进度条播放的实现(基于nginx)
http协议下的flv/mp4流式播放支持的三个要点: 1 服务器端要支持flv/mp4流式播放,现在nginx或者lighttpd都是支持这样的应用的,还支持mp4的流式播放(默认编译版本一般都是打 ...