-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLeetCode#130.cc
39 lines (33 loc) · 958 Bytes
/
LeetCode#130.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution{
public:
bool isMatch(const char *s, const char *p) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(!s && !p) return true;
const char *star_p=NULL,*star_s=NULL;
while(*s)
{
if(*p == '?' || *p == *s)
{
++p,++s;
}else if(*p == '*')
{
//skip all continuous '*'
while(*p == '*') ++p;
if(!*p) return true; //if end with '*', its match.
star_p = p; //store '*' pos for string and pattern
star_s = s;
}else if((!*p || *p != *s) && star_p)
{
s = ++star_s; //skip non-match char of string, regard it matched in '*'
p = star_p; //pattern backtrace to later char of '*'
}else
return false;
}
//check if later part of p are all '*'
while(*p)
if(*p++ != '*')
return false;
return true;
}
};