-
Notifications
You must be signed in to change notification settings - Fork 16
/
regularexpressionmatching.cpp
69 lines (56 loc) · 1.87 KB
/
regularexpressionmatching.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Solution {
public:
bool isMatch(string str, string pat) {
int n = str.size();
int m = pat.size();
// '.' can be any character
// "s*" can be replace with "", "s", "ss", "sss", "ssss" and so on .....
// above possibility can be replaced with "", "ss*"
// str in on i direction and pat is on j direction
// for the 0th row if we encounter '*' then we look for dp[0][j - 2]
vector<vector<bool>> dp(n + 1, vector<bool> (m + 1, false));
for(int i = 0; i <= n; i++)
{
for(int j = 0; j <= m ; j++)
{
if(i == 0 && j == 0)
{
dp[i][j] = true;
}
else if(j == 0)
{
dp[i][j] = false;
}
else if(i == 0)
{
if(pat[j - 1] == '*')
{
dp[i][j] = dp[i][j - 2];
}
else
{
dp[i][j] = false;
}
}
else
{
if(str[i - 1] == pat[j - 1] || pat[j - 1] == '.')
{
dp[i][j] = dp[i - 1][j - 1];
}
else if(pat[j - 1] == '*')
{
// eg. replace "mis*" with "mi"
dp[i][j] = dp[i][j - 2];
// eg. replace "mis*" with "miss*"
if(dp[i][j] == false && (str[i - 1] == pat[j - 2] || pat[j - 2] == '.'))
{
dp[i][j] = dp[i - 1][j];
}
}
}
}
}
return dp[n][m];
}
};