Skip to content

Commit 824dd0e

Browse files
C: add problem 10 and test function
1 parent b9f0fc3 commit 824dd0e

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

C/1-50/10-Regular-Expression-Matching.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,29 @@
2323
#include "../main.h"
2424
#include
2525

26-
bool isMatch(char* s, char* p) {
27-
if(p == NULL || *p){
28-
return *s;
26+
bool isMatch(char *s, char *p) {
27+
if (p == NULL || !*p) {
28+
return s == NULL || !*s;
2929
}
30+
if (s == NULL || !*s) {
31+
char *str = p;
32+
while (*str && str[1]) {
33+
if (str[1] != '*')
34+
return false;
35+
str += 2;
36+
}
37+
return *str ? false : true;
38+
}
39+
40+
if (p[1] == '*')
41+
return isMatch(s, p + 2) || p != NULL && *p && (s[0] == p[0] || p[0] == '.') && isMatch(s + 1, p);
42+
else
43+
return p != NULL && *p && (s[0] == p[0] || p[0] == '.') && isMatch(s + 1, p + 1);
3044

3145
}
46+
47+
void testIsMatch() {
48+
char *s = "aa";
49+
char *p = ".*c";
50+
printf("%d", isMatch(s, p));
51+
}

0 commit comments

Comments
 (0)