-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegularExpressionMatching.cpp
More file actions
36 lines (30 loc) · 938 Bytes
/
Copy pathRegularExpressionMatching.cpp
File metadata and controls
36 lines (30 loc) · 938 Bytes
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
//task 10
//0ms 9.1mb
class Solution {
public:
bool isMatch(string s, string p)
{
if (s.empty() && p.empty()) return true;
int sSize = s.size();
int pSize = p.size();
vector<vector<bool>> temp(sSize + 1, vector<bool>(pSize + 1, false));
temp[0][0] = true;
for (int i = 1; i <= sSize; ++i)
{
temp[i][0] == false;
}
for (int j = 0; j < pSize; ++j)
{
if(p[j] == '*' && j >= 1) temp[0][j+1] = temp[0][j-1];
}
for (int i = 0; i < sSize; ++i)
{
for (int j = 0; j < pSize; ++j)
{
if (p[j] != '*') temp[i+1][j+1] = temp[i][j] && (s[i] == p[j] || p[j] == '.');
else if (p[j] == '*' && j >= 1) temp[i+1][j+1] = temp[i+1][j-1] || (temp[i][j+1] && (p[j-1] == '.' || p[j-1] == s[i]));
}
}
return temp[sSize][pSize];
}
};