-
Notifications
You must be signed in to change notification settings - Fork 0
/
Regular_Expression_Matching.cpp
44 lines (37 loc) · 1.2 KB
/
Regular_Expression_Matching.cpp
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
/*
'.' 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", "a*") ¡ú true
isMatch("aa", ".*") ¡ú true
isMatch("ab", ".*") ¡ú true
isMatch("aab", "c*a*b") ¡ú true
*/
class Solution {
public:
bool isEqual(char s, char p) {
if (s == p || p == '.' && s != '\0') return true;
return false;
}
bool isMatch(const char *s, const char *p) {
if (*s == '\0' && *p == '\0') return true;
if (*p != '\0' && *(p + 1) == '*') {
// '*' represents zero of preceding element.
if (isMatch(s, p + 2)) return true;
// '*' represents one or more of preceding element.
while (isEqual(*s, *p)) {
++s;
if (isMatch(s, p + 2)) return true;
}
} else if (isEqual(*s, *p)) {
if (isMatch(s + 1, p + 1)) return true;
}
return false;
}
};