forked from royalpranjal/Interview-Bit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegularExpressionMatch.cpp
More file actions
98 lines (75 loc) · 2.19 KB
/
Copy pathRegularExpressionMatch.cpp
File metadata and controls
98 lines (75 loc) · 2.19 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
// https://www.interviewbit.com/problems/regular-expression-match/
int match(string s, int i, string p, int j){
if(i >= s.size() && j >= p.size()){
return 1;
}
if(i == s.size() && j < p.size()){
for(int t = j; t < p.size(); t++){
if(p[t] != '*'){
return 0;
}
}
return 1;
}
if(s[i] == p[j] || p[j] == '?'){
return match(s, i+1, p, j+1);
}
if(p[j] == '*'){
return max(match(s, i+1, p, j), match(s, i, p, j+1));
}
}
int Solution::isMatch(const string &s, const string &p) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
int pat = p.size();
int str = s.size();
int i = 0, j = 0;
int iIndex = -1, jIndex = -1;
while(i < str){
if(s[i] == p[j] || (j < pat && p[j] == '?')){
i++;
j++;
}
else if(p[j] == '*' && j < pat){
jIndex = j;
iIndex = i;
j++;
}
else if(jIndex != -1){
j = jIndex + 1;
i = iIndex + 1;
iIndex++;
}
else{
return 0;
}
}
while(j < pat && p[j] == '*'){
j++;
}
if(j == pat){
return 1;
}
return 0;
// return match(s, 0, p, 0);
// vector<vector<int> > temp(str+1, vector<int>(pat+1, 0));
// temp[0][0] = 1;
// for(int i = 1; i < temp[0].size(); i++){
// if(p[i-1] == '*'){
// temp[0][i] = temp[0][i-1];
// }
// }
// for(int i = 1; i < temp.size(); i++){
// for(int j = 1; j < temp[i].size(); j++){
// if(p[j-1] == s[i-1] || p[j-1] == '?'){
// temp[i][j] = temp[i-1][j-1];
// }
// else if(p[j-1] == '*'){
// temp[i][j] = max(temp[i-1][j], temp[i][j-1]);
// }
// }
// }
// return temp[str][pat];
}