/
10. Regular Expression Matching.cpp
57 lines (51 loc) · 1.54 KB
/
10. 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
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* problem : https://leetcode.com/problems/regular-expression-matching/
* time complexity : O(N^2). let, N = |s|
* algorithm : DP, backtracking
*/
class Solution {
vector<vector<int>> dp;
bool isEqual(char s, char p){
if(p == '.')
return true;
return s == p;
}
bool isMatch(string& s, string& p, int sInd, int pInd) {
if(sInd >= s.size()) {
bool ans = true;
for(int i=pInd; i<p.size(); i++){
if(i+1 < p.size() && p[i+1] == '*') {
i++;
continue;
}
ans = false; break;
}
return ans;
}
if(pInd >= p.size()) {
return false;
}
int& ret = dp[sInd][pInd];
if(ret != -1) return ret;
ret = false;
if(pInd + 1 < p.size() && p[pInd+1] == '*') {
ret = isMatch(s, p, sInd, pInd+2);
for(int i=sInd; i<=s.size();i++){
if(!isEqual(s[i], p[pInd])) break;
ret |= isMatch(s, p, i+1, pInd+2);
}
} else if (p[pInd] == '.') {
ret = isMatch(s, p, sInd+1, pInd+1);
} else {
if(s[sInd] != p[pInd]) ret = false;
else ret = isMatch(s, p, sInd+1, pInd+1);
}
return ret;
}
public:
bool isMatch(string s, string p) {
dp = vector<vector<int>>(s.size(), vector<int>(p.size(), -1));
bool answer = isMatch(s, p, 0, 0);
return answer;
}
};