-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.regular-expression-matching.c
246 lines (236 loc) · 6.72 KB
/
10.regular-expression-matching.c
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* @lc app=leetcode.cn id=10 lang=c
*
* [10] Regular Expression Matching
*
* https://leetcode.cn/problems/regular-expression-matching/description/
*
* algorithms
* Hard (30.71%)
* Likes: 3937
* Dislikes: 0
* Total Accepted: 428.9K
* Total Submissions: 1.4M
* Testcase Example: '"aa"\n"a"'
*
* Given an input string s and a pattern p, implement regular expression
* matching with support for '.' and '*' where:
*
*
* '.' Matches any single character.
* '*' Matches zero or more of the preceding element.
*
*
* The matching should cover the entire input string (not partial).
*
*
* Example 1:
*
*
* Input: s = "aa", p = "a"
* Output: false
* Explanation: "a" does not match the entire string "aa".
*
*
* Example 2:
*
*
* Input: s = "aa", p = "a*"
* Output: true
* Explanation: '*' means zero or more of the preceding element, 'a'.
* Therefore, by repeating 'a' once, it becomes "aa".
*
*
* Example 3:
*
*
* Input: s = "ab", p = ".*"
* Output: true
* Explanation: ".*" means "zero or more (*) of any character (.)".
*
*
*
* Constraints:
*
*
* 1 <= s.length <= 20
* 1 <= p.length <= 20
* s contains only lowercase English letters.
* p contains only lowercase English letters, '.', and '*'.
* It is guaranteed for each appearance of the character '*', there will be a
* previous valid character to match.
*
*
*/
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
// @lc code=start
typedef enum pattern_type
{
CHARS, // eg: ccc.ccc
CHAR_COUNTLESS, // eg: c*
ANY_CHAR_COUNTLESS // .*
} pattern_type;
typedef struct sub_pattern
{
enum pattern_type type;
char *pattern;
int pattern_size;
} sub_pattern;
bool is_match(char *s, int start, int s_len, struct sub_pattern **sub_ps, int sub_ps_idx, int sub_ps_len,
int **records)
{
if (sub_ps_idx == sub_ps_len)
{
return start == s_len;
}
if (start == s_len)
{
for (int i = sub_ps_idx; i < sub_ps_len; i++)
{
if (sub_ps[i]->type == CHARS)
{
return false;
}
}
return true;
}
if (records[start][sub_ps_idx] != -1)
{
return records[start][sub_ps_idx] == 1;
}
int s_idx;
struct sub_pattern *sub_p = sub_ps[sub_ps_idx];
switch (sub_p->type)
{
case CHARS:
s_idx = start;
for (int i = 0; i < sub_p->pattern_size; i++)
{
if (s_idx == s_len)
{
records[start][sub_ps_idx] = 0;
return false;
}
if (sub_p->pattern[i] != s[s_idx] && sub_p->pattern[i] != '.')
{
records[start][sub_ps_idx] = 0;
return false;
}
s_idx++;
}
return is_match(s, s_idx, s_len, sub_ps, sub_ps_idx + 1, sub_ps_len, records);
case CHAR_COUNTLESS:
if (is_match(s, start, s_len, sub_ps, sub_ps_idx + 1, sub_ps_len, records))
{
records[start][sub_ps_idx] = 1;
return true;
}
s_idx = start;
while (1)
{
if (s[s_idx++] == sub_p->pattern[0])
{
if (is_match(s, s_idx, s_len, sub_ps, sub_ps_idx + 1, sub_ps_len, records))
{
records[start][sub_ps_idx] = 1;
return true;
}
}
else
{
records[start][sub_ps_idx] = 0;
return false;
}
}
default:
s_idx = start;
while (s_idx <= s_len)
{
if (is_match(s, s_idx, s_len, sub_ps, sub_ps_idx + 1, sub_ps_len, records))
{
records[start][sub_ps_idx] = 1;
return true;
}
else
{
s_idx++;
}
}
records[start][sub_ps_idx] = 0;
return false;
}
}
bool isMatch(char *s, char *p)
{
int p_len = strlen(p);
struct sub_pattern **sub_p = (struct sub_pattern **)malloc(sizeof(struct sub_pattern *) * p_len);
int sub_p_len = 0;
int idx_p = 0, idx_p_next = 0;
while (1)
{
if (idx_p_next == p_len)
{
if (idx_p_next == idx_p)
{
break;
}
sub_p[sub_p_len++] = (struct sub_pattern *)malloc(sizeof(struct sub_pattern));
sub_p[sub_p_len - 1]->type = CHARS;
sub_p[sub_p_len - 1]->pattern_size = idx_p_next - idx_p;
sub_p[sub_p_len - 1]->pattern = (char *)malloc(sizeof(char) * (sub_p[sub_p_len - 1]->pattern_size + 1));
memcpy(sub_p[sub_p_len - 1]->pattern, p + idx_p, sizeof(char) * sub_p[sub_p_len - 1]->pattern_size);
sub_p[sub_p_len - 1]->pattern[sub_p[sub_p_len - 1]->pattern_size] = '\0';
break;
}
if (p[idx_p_next] != '*')
{
idx_p_next++;
}
else
{
if (idx_p_next > idx_p + 1)
{
sub_p[sub_p_len++] = (struct sub_pattern *)malloc(sizeof(struct sub_pattern));
sub_p[sub_p_len - 1]->type = CHARS;
sub_p[sub_p_len - 1]->pattern_size = idx_p_next - idx_p - 1;
sub_p[sub_p_len - 1]->pattern = (char *)malloc(sizeof(char) * (sub_p[sub_p_len - 1]->pattern_size + 1));
memcpy(sub_p[sub_p_len - 1]->pattern, p + idx_p, sizeof(char) * sub_p[sub_p_len - 1]->pattern_size);
sub_p[sub_p_len - 1]->pattern[sub_p[sub_p_len - 1]->pattern_size] = '\0';
idx_p = idx_p_next - 1;
}
sub_p[sub_p_len++] = (struct sub_pattern *)malloc(sizeof(struct sub_pattern));
sub_p[sub_p_len - 1]->type = p[idx_p] == '.' ? ANY_CHAR_COUNTLESS : CHAR_COUNTLESS;
if (sub_p[sub_p_len - 1]->type == CHAR_COUNTLESS)
{
sub_p[sub_p_len - 1]->pattern_size = 1;
sub_p[sub_p_len - 1]->pattern = (char *)malloc(sizeof(char) * 2);
sub_p[sub_p_len - 1]->pattern[0] = p[idx_p_next - 1];
sub_p[sub_p_len - 1]->pattern[1] = '\0';
}
idx_p = idx_p_next + 1;
idx_p_next++;
}
}
int s_len = strlen(s);
int **records = (int **)malloc(sizeof(int *) * s_len);
for (int i = 0; i < s_len; i++)
{
records[i] = (int *)malloc(sizeof(int) * sub_p_len);
for (int j = 0; j < sub_p_len; j++)
{
records[i][j] = -1;
}
}
return is_match(s, 0, s_len, sub_p, 0, sub_p_len, records);
}
// @lc code=end
int main(int argc, char const *argv[])
{
char *s = "aaaaaaaaaaaaab";
char *p = "a*a*a*a*a*a*a*a*a*c";
printf("march %d", isMatch(s, p));
return 0;
}