forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths5.cpp
More file actions
21 lines (21 loc) · 769 Bytes
/
s5.cpp
File metadata and controls
21 lines (21 loc) · 769 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// OJ: https://leetcode.com/problems/regular-expression-matching/
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(MN)
// Ref: https://leetcode.com/problems/regular-expression-matching/solution/
class Solution {
public:
bool isMatch(string s, string p) {
int M = s.size(), N = p.size();
vector<vector<bool>> dp(M + 1, vector<bool>(N + 1));
dp[M][N] = true;
for (int i = M; i >= 0; --i) {
for (int j = N - 1; j >= 0; --j) {
bool match = i < M && (p[j] == '.' || p[j] == s[i]);
if (j + 1 < N && p[j + 1] == '*') dp[i][j] = dp[i][j + 2] || (match && dp[i + 1][j]);
else dp[i][j] = match && dp[i + 1][j + 1];
}
}
return dp[0][0];
}
};