Skip to content

Commit ce5e0f0

Browse files
add problem 44 and test function
1 parent 37925b9 commit ce5e0f0

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

C/1-50/44-Wildcard-Matching.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Implement wildcard pattern matching with support for '?' and '*'.
3+
* '?' Matches any single character.
4+
* '*' Matches any sequence of characters (including the empty sequence).
5+
6+
* The matching should cover the entire input string (not partial).
7+
8+
* The function prototype should be:
9+
* bool isMatch(const char *s, const char *p)
10+
11+
* Some examples:
12+
* isMatch("aa","a") ? false
13+
* isMatch("aa","aa") ? true
14+
* isMatch("aaa","aa") ? false
15+
* isMatch("aa", "*") ? true
16+
* isMatch("aa", "a*") ? true
17+
* isMatch("ab", "?*") ? true
18+
* isMatch("aab", "c*a*b") ? false
19+
* Created by supercoderx on 2017/8/14.
20+
*/
21+
#include "../main.h"
22+
23+
#include
24+
25+
bool isWildcardMatch(char *s, char *p) {
26+
int patternLen = 0, ques = 0,asterisk =0;
27+
char *star = NULL, *ss = s, *tmp = p;
28+
bool all = true, other = false;
29+
while (*tmp != '\0') {
30+
patternLen++;
31+
if (*tmp != '*')
32+
all = false;
33+
if (*tmp == '?')
34+
ques++;
35+
if (*tmp != '?' && *tmp != '*')
36+
other = true;
37+
tmp++;
38+
}
39+
if (*s == '\0' && (other || ques > 0))
40+
return false;
41+
if (*s == '\0' && (patternLen > 0 && (all || !other && ques == 1)))
42+
return true;
43+
while (*s != '\0') {
44+
if (*p == '?' || (*p == *s)) {
45+
s++;
46+
p++;
47+
continue;
48+
}
49+
if (*p == '*') {
50+
star = p++;
51+
ss = s;
52+
continue;
53+
}
54+
if (star) {
55+
p = star + 1;
56+
s = ++ss;
57+
continue;
58+
}
59+
return false;
60+
}
61+
while(*p!='\0'){
62+
if(*p!='*')
63+
return false;
64+
p++;
65+
}
66+
return true;
67+
}
68+
69+
void testIsWildcardMatch() {
70+
printf("%d", isWildcardMatch("a", "?*?"));
71+
}

0 commit comments

Comments
 (0)