Skip to content

Commit 01e1e53

Browse files
authored
Update README.md
1 parent 3edfb3d commit 01e1e53

File tree

1 file changed

+58
-25
lines changed
  • solution/0000-0099/0010.Regular Expression Matching

1 file changed

+58
-25
lines changed

solution/0000-0099/0010.Regular Expression Matching/README.md

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,44 @@ public class Solution {
331331
}
332332
```
333333

334+
#### C
335+
336+
```c
337+
#define MAX_LEN 1000
338+
339+
char *ss, *pp;
340+
int m, n;
341+
int f[MAX_LEN + 1][MAX_LEN + 1];
342+
343+
bool dfs(int i, int j) {
344+
if (j >= n) {
345+
return i == m;
346+
}
347+
if (f[i][j] != 0) {
348+
return f[i][j] == 1;
349+
}
350+
int res = -1;
351+
if (j + 1 < n && pp[j + 1] == '*') {
352+
if (dfs(i, j + 2) || (i < m && (ss[i] == pp[j] || pp[j] == '.') && dfs(i + 1, j))) {
353+
res = 1;
354+
}
355+
} else if (i < m && (ss[i] == pp[j] || pp[j] == '.') && dfs(i + 1, j + 1)) {
356+
res = 1;
357+
}
358+
f[i][j] = res;
359+
return res == 1;
360+
}
361+
362+
bool isMatch(char* s, char* p) {
363+
ss = s;
364+
pp = p;
365+
m = strlen(s);
366+
n = strlen(p);
367+
memset(f, 0, sizeof(f));
368+
return dfs(0, 0);
369+
}
370+
```
371+
334372
<!-- tabs:end -->
335373
336374
<!-- solution:end -->
@@ -581,31 +619,26 @@ class Solution {
581619

582620
#### C
583621

584-
```C
585-
bool isMatch(char *s, char *p) {
586-
int m = strlen(s), n = strlen(p);
587-
bool **dp = malloc((m + 1) * sizeof(bool *));
588-
for (int i = 0; i <= m; i++)
589-
dp[i] = calloc(n + 1, sizeof(bool));
590-
dp[0][0] = 1;
591-
for (int j = 2; j <= n; j++)
592-
if (p[j - 1] == '*')
593-
dp[0][j] = dp[0][j - 2];
594-
595-
for (int i = 1; i <= m; i++)
596-
for (int j = 1; j <= n; j++)
597-
if (p[j - 1] == '*')
598-
dp[i][j] = dp[i][j - 2] ||
599-
((p[j - 2] == '.' || p[j - 2] == s[i - 1]) && dp[i - 1][j]);
600-
else
601-
dp[i][j] =
602-
(p[j - 1] == '.' || p[j - 1] == s[i - 1]) && dp[i - 1][j - 1];
603-
604-
bool res = dp[m][n];
605-
for (int i = 0; i <= m; i++)
606-
free(dp[i]);
607-
free(dp);
608-
return res;
622+
```c
623+
bool isMatch(char* s, char* p) {
624+
int m = strlen(s), n = strlen(p);
625+
bool f[m + 1][n + 1];
626+
memset(f, 0, sizeof(f));
627+
f[0][0] = true;
628+
629+
for (int i = 0; i <= m; ++i) {
630+
for (int j = 1; j <= n; ++j) {
631+
if (p[j - 1] == '*') {
632+
f[i][j] = f[i][j - 2];
633+
if (i > 0 && (p[j - 2] == '.' || p[j - 2] == s[i - 1])) {
634+
f[i][j] = f[i][j] || f[i - 1][j];
635+
}
636+
} else if (i > 0 && (p[j - 1] == '.' || p[j - 1] == s[i - 1])) {
637+
f[i][j] = f[i - 1][j - 1];
638+
}
639+
}
640+
}
641+
return f[m][n];
609642
}
610643
```
611644

0 commit comments

Comments
 (0)