-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.cc
28 lines (27 loc) · 1002 Bytes
/
10.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
class Solution {
public:
bool isMatch(string text, string pattern) {
if(text == pattern) return true;
if(pattern == "") return false;
if(pattern[1] == '?')
return matchQuestion(text, pattern);
if(pattern[1] == '*')
return matchStar(text, pattern);
return matchOne(text[0], pattern[0]) && isMatch(text.substr(1), pattern.substr(1));
}
bool matchOne(char text, char pattern) {
if(!pattern) return true;
if(!text) return false;
if(pattern == '.') return true;
if(pattern == text) return true;
return false;
}
bool matchStar(string text, string pattern) {
return (matchOne(text[0], pattern[0])) && isMatch(text.substr(1), pattern) || isMatch(text, pattern.substr(2));
}
bool matchQuestion(string text, string pattern) {
return matchOne(text[0], pattern[0])
&& isMatch(text.substr(1), pattern.substr(2))
|| isMatch(text, pattern.substr(2));
}
};