-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path10. Regular Expression Matching.cpp
55 lines (47 loc) · 1.37 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
// ____ _ _ _ _ __ __ _ _ _
// | _ \(_)___| |__ __ _| |__ | |__ | \/ | __ _| | |__ ___ | |_ _ __ __ _
// | |_) | / __| '_ \ / _` | '_ \| '_ \ | |\/| |/ _` | | '_ \ / _ \| __| '__/ _` |
// | _ <| \__ \ | | | (_| | |_) | | | | | | | | (_| | | | | | (_) | |_| | | (_| |
// |_| \_\_|___/_| |_|\__,_|_.__/|_| |_| |_| |_|\__,_|_|_| |_|\___/ \__|_| \__,_|
#include <bits/stdc++.h>
using namespace std;
// Dynamic Programming | Strings | ⭐⭐⭐⭐⭐ | Decision Making
class Solution
{
public:
bool isMatch(string s, string p)
{
int n = p.size();
int m = s.size();
vector<vector<bool>> dp(n + 1, vector<bool>(m + 1, false));
dp[0][0] = true;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= m; j++)
{
if (j == 0)
{
dp[i][j] = (p[i - 1] == '*' ? dp[i - 2][j] : false);
}
else
{
if (p[i - 1] == '.')
{
dp[i][j] = dp[i - 1][j - 1];
}
else if (p[i - 1] == '*')
{
bool nil = dp[i - 2][j];
bool add = (p[i - 2] == s[j - 1] || p[i - 2] == '.') && dp[i][j - 1];
dp[i][j] = (nil || add);
}
else
{
dp[i][j] = (p[i - 1] == s[j - 1] ? dp[i - 1][j - 1] : false);
}
}
}
}
return dp[n][m];
}
};