-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWildcardMatchingLecture34.cpp
More file actions
145 lines (143 loc) · 3.25 KB
/
Copy pathWildcardMatchingLecture34.cpp
File metadata and controls
145 lines (143 loc) · 3.25 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
// https://bit.ly/3qXtORt
bool solve1(int i,int j,string &pattern,string &text)
{
if(i < 0 && j < 0) return true;
if(i < 0 && j >= 0) return false;
if(j < 0 && i >= 0)
{
for (int ii = 0; ii <= i; ii++) {
if (pattern[ii] != '*')
return false;
}
return true;
}
if(pattern[i] == text[j] || pattern[i] == '?')
{
return solve1(i-1,j-1,pattern,text);
}
if(pattern[i] == '*')
{
return solve1(i-1,j,pattern,text) | solve1(i,j-1,pattern,text);
}
return false;
}
bool solve2(int i,int j,string &pattern,string &text,
vector<vector<int>> &dp)
{ // 1 - Based Indexing
if(i == 0 && j == 0) return true;
if(i == 0 && j > 0) return false;
if(j == 0 && i > 0)
{
for (int ii = 1; ii <= i; ii++) {
if (pattern[ii-1] != '*')
return false;
}
return true;
}
if(dp[i][j] != -1) return dp[i][j];
if(pattern[i-1] == text[j-1] || pattern[i-1] == '?')
{
return dp[i][j] = solve2(i-1,j-1,pattern,text,dp);
}
if(pattern[i-1] == '*')
{
return dp[i][j] = (solve2(i-1,j,pattern,text,dp) | solve2(i,j-1,pattern,text,dp));
}
return dp[i][j] = false;
}
bool solve3(string pattern, string text)
{
int n = pattern.size();
int m = text.size();
vector<vector<bool>> dp(n+1,vector<bool>(m+1,-1));
dp[0][0] = true;
for(int j = 1;j <= m;j++)
{
dp[0][j] = false;
}
for(int i = 1;i <= n;i++)
{
bool flag = true;
for(int ii = 1;ii <= i;ii++)
{
if (pattern[ii - 1] != '*')
{
flag = false;
break;
}
}
dp[i][0] = flag;
}
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= m;j++)
{
if(pattern[i-1] == text[j-1] || pattern[i-1] == '?')
{
dp[i][j] = dp[i-1][j-1];
}
else if(pattern[i-1] == '*')
{
dp[i][j] = (dp[i-1][j] | dp[i][j-1]);
}
else dp[i][j] = false;
}
}
return dp[n][m];
}
bool solve4(string pattern, string text)
{
int n = pattern.size();
int m = text.size();
vector<bool> prev(m+1,0);
vector<bool> curr(m+1,0);
prev[0] = true;
for(int j = 1;j <= m;j++)
{
prev[j] = false;
}
for(int i = 1;i <= n;i++)
{
bool flag = true;
for(int ii = 1;ii <= i;ii++)
{
if (pattern[ii - 1] != '*')
{
flag = false;
break;
}
}
// for every row
// you are assigning the 0th column's value
curr[0] = flag;
for(int j = 1;j <= m;j++)
{
if(pattern[i-1] == text[j-1] || pattern[i-1] == '?')
{
curr[j] = prev[j-1];
}
else if(pattern[i-1] == '*')
{
curr[j] = (prev[j] | curr[j-1]);
}
else curr[j] = false;
}
prev = curr;
}
return prev[m];
}
bool wildcardMatching(string pattern, string text)
{
int n = pattern.size();
int m = text.size();
vector<vector<int>> dp(n+1,vector<int>(m+1,-1));
// return solve1(n-1,m-1,pattern,text);
// return solve2(n,m,pattern,text,dp);
// return solve3(pattern,text);
return solve4(pattern,text);
}