forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegularExpressionMatching.cpp
More file actions
64 lines (64 loc) · 1.72 KB
/
RegularExpressionMatching.cpp
File metadata and controls
64 lines (64 loc) · 1.72 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Solution {
public:
bool isMatch(const char *s, const char *p) {
if(s==NULL || p==NULL) {
return false;
}
if(strlen(p)==0 && strlen(s)==0) {
return true;
}
if(strlen(p)==0 && strlen(s)>0) {
return false;
}
if(strlen(p)==1) {
if(p[0]=='*') {
return false;
}
else if(strlen(s)==0) {
return false;
}
else if(p[0]=='.') {
return isMatch(s+1, p+1);
}
else {
return p[0] == s[0] && isMatch(s+1,p+1);
}
}
else {
if(p[0] == '.' && p[1] == '*') {
while(true) {
if(isMatch(s, p+2)) {
return true;
}
if(*s=='\0') {
return isMatch(s,p+2);
}
s++;
}
return false;
} else if(p[1] == '*') {
char t = p[0];
if(isMatch(s,p+2)) {
return true;
}
while(*s == t) {
if(isMatch(s+1,p+2)) {
return true;
}
s++;
}
return false;
} else if(p[0] == '.') {
if(strlen(s)==0) {
return false;
}
return isMatch(s+1, p+1);
} else {
if(strlen(s)==0) {
return false;
}
return (s[0] == p[0]) && isMatch(s+1,p+1);
}
}
}
};