-
Notifications
You must be signed in to change notification settings - Fork 0
/
00010-regular-expression-matching.cpp
49 lines (45 loc) · 1.51 KB
/
00010-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
/* hard :: dynamic-programming */
/*
Build a dynamic programming table and update it based
on the matching rules. Return whether the entire string
matches the pattern string.
- - - -
Time :: O(m * n)
Space :: O(m * n)
*/
class Solution {
public:
bool isMatch(string s, string p)
{
int m = s.length(), n = p.length();
// Create a DP table, initialized with false ::
vector<vector<bool>> dp(m+1, vector<bool>(n+1, false));
// Empty string and empty pattern always match ::
dp[0][0] = true;
// Handle patterns like a*, a*b*, a*b*c*, etc ::
for (int j = 1; j <= n; j++) {
if (p[j-1] == '*') {
dp[0][j] = dp[0][j-2];
}
}
// Fill the DP table ::
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (p[j-1] == '.' || p[j-1] == s[i-1]) {
dp[i][j] = dp[i-1][j-1];
}
else if (p[j-1] == '*') {
// Zero occurrence of preceding element ::
dp[i][j] = dp[i][j-2];
if (p[j-2] == '.' || p[j-2] == s[i-1]) {
// One or more occurrence of preceding element ::
dp[i][j] = dp[i][j] || dp[i-1][j];
}
}
}
}
return dp[m][n];
}
};