Leetcode 10. Regular Expression Matching

https://leetcode.com/problems/regular-expression-matching/

题意:正则表达式匹配

代码:

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
class Solution {
public:
bool isMatch(string s, string p) {
if(p.empty()){
return s.empty();
}

if(p.size() == 1){
return (s.size() == 1 && (s[0] == p[0] || p[0] == '.'));
}

if(p[1] != '*'){
if(s.empty())
return false;
return (s[0] == p[0] || p[0] == '.') && isMatch(s.substr(1), p.substr(1));
}

while(!s.empty() && (s[0] == p[0] || p[0] == '.')){
if(isMatch(s, p.substr(2)))
return true;
s = s.substr(1);
}

return isMatch(s, p.substr(2));
}
};

0%