@@ -330,6 +330,44 @@ public class Solution {
330330}
331331```
332332
333+ #### C
334+
335+ ``` c
336+ #define MAX_LEN 1000
337+
338+ char *ss, *pp;
339+ int m, n;
340+ int f[MAX_LEN + 1 ][MAX_LEN + 1 ];
341+
342+ bool dfs (int i, int j) {
343+ if (j >= n) {
344+ return i == m;
345+ }
346+ if (f[ i] [ j ] != 0) {
347+ return f[ i] [ j ] == 1;
348+ }
349+ int res = -1;
350+ if (j + 1 < n && pp[ j + 1] == '* ') {
351+ if (dfs(i, j + 2) || (i < m && (ss[ i] == pp[ j] || pp[ j] == '.') && dfs(i + 1, j))) {
352+ res = 1;
353+ }
354+ } else if (i < m && (ss[ i] == pp[ j] || pp[ j] == '.') && dfs(i + 1, j + 1)) {
355+ res = 1;
356+ }
357+ f[ i] [ j ] = res;
358+ return res == 1;
359+ }
360+
361+ bool isMatch(char* s, char* p) {
362+ ss = s;
363+ pp = p;
364+ m = strlen(s);
365+ n = strlen(p);
366+ memset(f, 0, sizeof(f));
367+ return dfs(0, 0);
368+ }
369+ ```
370+
333371<!-- tabs:end -->
334372
335373<!-- solution:end -->
@@ -581,30 +619,25 @@ class Solution {
581619#### C
582620
583621``` C
584- bool isMatch (char * s, char * p) {
585- int m = strlen(s), n = strlen(p);
586- bool ** dp = malloc((m + 1) * sizeof(bool * ));
587- for (int i = 0; i <= m; i++)
588- dp[ i] = calloc(n + 1, sizeof(bool));
589- dp[ 0] [ 0 ] = 1;
590- for (int j = 2; j <= n; j++)
591- if (p[ j - 1] == '* ')
592- dp[ 0] [ j ] = dp[ 0] [ j - 2 ] ;
593-
594- for (int i = 1; i <= m; i++)
595- for (int j = 1; j <= n; j++)
596- if (p[ j - 1] == '* ')
597- dp[ i] [ j ] = dp[ i] [ j - 2 ] ||
598- ((p[ j - 2] == '.' || p[ j - 2] == s[ i - 1] ) && dp[ i - 1] [ j ] );
599- else
600- dp[ i] [ j ] =
601- (p[ j - 1] == '.' || p[ j - 1] == s[ i - 1] ) && dp[ i - 1] [ j - 1 ] ;
602-
603- bool res = dp[ m] [ n ] ;
604- for (int i = 0; i <= m; i++)
605- free(dp[ i] );
606- free(dp);
607- return res;
622+ bool isMatch (char* s, char* p) {
623+ int m = strlen(s), n = strlen(p);
624+ bool f[ m + 1] [ n + 1 ] ;
625+ memset(f, 0, sizeof(f));
626+ f[ 0] [ 0 ] = true;
627+
628+ for (int i = 0; i <= m; ++i) {
629+ for (int j = 1; j <= n; ++j) {
630+ if (p[j - 1] == '*') {
631+ f[i][j] = f[i][j - 2];
632+ if (i > 0 && (p[j - 2] == '.' || p[j - 2] == s[i - 1])) {
633+ f[i][j] = f[i][j] || f[i - 1][j];
634+ }
635+ } else if (i > 0 && (p[j - 1] == '.' || p[j - 1] == s[i - 1])) {
636+ f[i][j] = f[i - 1][j - 1];
637+ }
638+ }
639+ }
640+ return f[m][n];
608641}
609642```
610643
0 commit comments