-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10-regular-expression-matching.cpp
147 lines (136 loc) · 3.5 KB
/
10-regular-expression-matching.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
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
146
147
class Solution {
public:
bool isMatch(string s, string p) {
int s_i = 0;
int p_i = 0;
int s_len = s.length();
int p_len = p.length();
if (s_len == 0 && p_len == 0)
{
return true;
}
bool flag = true;
while (s_i < s_len && p_i < p_len)
{
if (!flag && p[p_i] != '*')
{
return false;
}
if (!flag && p[p_i] == '*')
{
p_i++;
flag = true;
continue;
}
if (s[s_i] == p[p_i])
{
if (p_i == p_len - 1 && s_i == s_len - 1)
{
return true;
}
if (p[p_i + 1] != '*')
{
s_i++;
}
p_i++;
flag = true;
continue;
}
if (p[p_i] == '.')
{
if (p_i == p_len - 1 && s_i == s_len - 1)
{
return true;
}
if (p[p_i + 1] != '*')
{
s_i++;
}
p_i++;
flag = true;
continue;
}
if (p[p_i] == '*')
{
if (p_i == 0)
{
return false;
}
char temp = p[p_i - 1];
while (p[p_i] == '*')
{
p_i++;
}
string next_p = p.substr(p_i);
if (temp == '.')
{
if (p_i == p_len)
{
return true;
}
while (s_i <= s_len)
{
bool val = isMatch(s.substr(s_i), next_p);
if (val)
{
return true;
}
s_i++;
}
return false;
}
while (s_i < s_len)
{
bool val = isMatch(s.substr(s_i), next_p);
if (val)
{
return true;
}
s_i++;
if (s[s_i] != temp)
{
break;
}
}
continue;
}
flag = false;
p_i++;
}
if (s_i == s_len && p_i == p_len)
{
return true;
}
if (s_i == s_len)
{
bool _flag = true;
while (p_i < p_len)
{
if (!_flag && p[p_i] != '*')
{
return false;
}
if (p[p_i] == '*')
{
_flag = true;
p_i++;
continue;
}
_flag = false;
p_i++;
}
if (_flag)
{
return true;
}
else
{
return false;
}
}
if (p_i == p_len)
{
return false;
}
}
};