-
Notifications
You must be signed in to change notification settings - Fork 0
/
010_RegularExpressionMatching_DynamicProgramming.c
75 lines (66 loc) · 1.37 KB
/
010_RegularExpressionMatching_DynamicProgramming.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
bool isMatch(const char *s, const char *p) {
int i = 0, j = 0;
int sLen = strlen(s);
int pLen = strlen(p);
bool res = false;
bool **table = NULL;
if(sLen == 0 && pLen == 0)
return true;
if(pLen == 0)
return false;
// Initialize table with false.
table = (bool **)malloc((pLen + 1) * sizeof(bool*));
for(j = 0; j <= pLen; j++) {
*(table + j) = (bool *)malloc((sLen + 1) * sizeof(bool));
}
for(j = 0; j <= pLen; j++) {
for(i = 0; i <= sLen; i++) {
table[j][i] = false;
}
}
table[0][0] = true;
i = 0;
j = 0;
for(j = 0; j < pLen; j++) {
if(p[j] != '*') {
for(i = 0; i < sLen; i++) {
if(s[i] == p[j] || p[j] == '.') {
table[j + 1][i + 1] = table[j][i];
}
}
}
else {
if(j > 0) {
if(table[j - 1][0])
table[j + 1][0] = true;
if(p[j - 1] == '.') {
i = 0;
while(i < sLen && !table[j - 1][i + 1]
&& !table[j][i + 1]) {
i++;
}
for(; i < sLen; i++) {
table[j + 1][i + 1] = true;
}
}
else {
for(i = 0; i < sLen; i++) {
if(table[j - 1][i + 1] || table[j][i + 1]
|| i > 0 && j > 0 && table[j + 1][i]
&& s[i] == s[i - 1] && s[i - 1] == p[j - 1]) {
table[j + 1][i + 1] = true;
}
}
}
}
}
}
res = table[pLen][sLen];
for(j = 0; j <= pLen; j++) {
free(*(table + j));
*(table + j) = NULL;
}
free(table);
table = NULL;
return res;
}