您的位置:首页 > 其它

115-Wildcard Matching

2016-07-01 00:27 274 查看
-44. Wildcard Matching My Submissions QuestionEditorial Solution

Total Accepted: 60794 Total Submissions: 341535 Difficulty: Hard

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

有难度,思考整个自动机的运转才能写出无bug的代码

class Solution {
public:
bool isMatch(string s, string p) {
return isMatch(s.c_str(),p.c_str());
}
bool isMatch(const char *s,const char *p){
bool star = false;
const char *str,*ptr;
for(str=s,ptr=p;*str!='\0';str++,ptr++){
switch(*ptr){
case '?':
break;
case '*':
star = true;
s = str, p =ptr;
while(*p=='*')p++;
if(*p=='\0')return true;
str = s-1;
ptr = p -1;
break;
default:
if(*str!=*ptr){
if(!star)return false;
s++;
str = s-1;
ptr = p-1;
}
}
}
while(*ptr=='*')ptr++;
return *ptr=='\0';
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: