forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0010-regular-expression-matching.cpp
49 lines (41 loc) · 1.31 KB
/
0010-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
/*
Given string & pattern, implement RegEx matching
'.' -> matches any single character
'*' -> matches zero or more of the preceding element
Matching should cover the entire input string (not partial)
Ex. s = "aa", p = "a" -> false, "a" doesn't match entire string "aa"
DFS + memo, 2 choices at a *: either use it, or don't use it
Time: O(m x n)
Space: O(m x n)
*/
class Solution {
public:
bool isMatch(string s, string p) {
return dfs(s, p, 0, 0);
}
private:
map<pair<int, int>, bool> dp;
bool dfs(string& s, string& p, int i, int j) {
if (dp.find({i, j}) != dp.end()) {
return dp[{i, j}];
}
if (i >= s.size() && j >= p.size()) {
return true;
}
if (j >= p.size()) {
return false;
}
bool match = i < s.size() && (s[i] == p[j] || p[j] == '.');
if (j + 1 < p.size() && p[j + 1] == '*') {
// choices: either (1) don't use *, or (2) use *
dp[{i, j}] = dfs(s, p, i, j + 2) || (match && dfs(s, p, i + 1, j));
return dp[{i, j}];
}
if (match) {
dp[{i, j}] = dfs(s, p, i + 1, j + 1);
return dp[{i, j}];
}
dp[{i, j}] = false;
return dp[{i, j}];
}
};