File tree Expand file tree Collapse file tree 1 file changed +17
-17
lines changed Expand file tree Collapse file tree 1 file changed +17
-17
lines changed Original file line number Diff line number Diff line change @@ -33,34 +33,34 @@ using namespace std;
3333
3434bool isMatch (const char *s, const char *p) {
3535
36- bool star = false ;
37- const char *s1, *p1 ;
38- while ( *s && (*p || star) ){
36+ const char *last_s = NULL ;
37+ const char *last_p = NULL ;
38+ while ( *s != ' \0 ' ){
3939 if (*p==' *' ){
4040 // skip the "*", and mark a flag
41- star = true ;
4241 p++;
4342 // edge case
4443 if (*p==' \0 ' ) return true ;
45- // use s1 and p1 to store where the "*" match starts.
46- s1 = s;
47- p1 = p;
44+ // use last_s and last_p to store where the "*" match starts.
45+ last_s = s;
46+ last_p = p;
4847 }else if (*p==' ?' || *s == *p){
4948 s++; p++;
50- }else {
51- if (star==false ) return false ;
52- // if meet "*" previously, but the *s != *p
49+ }else if (last_s != NULL ){ // check "last_s" to know whether meet "*" before
50+ // if meet "*" previously, and the *s != *p
5351 // reset the p, using '*' to match this situation
54- p = p1;
55- s = ++s1;
52+ p = last_p;
53+ s = ++last_s;
54+ }else {
55+ // *p is not wildcard char,
56+ // doesn't match *s,
57+ // there are no '*' wildcard matched before
58+ return false ;
5659 }
5760 }
5861 // edge case: "s" is done, but "p" still have chars.
59- if (*s==' \0 ' ) {
60- while (*p==' *' ) p++; // filter all of '*'
61- if (*p==' \0 ' ) return true ;
62- }
63- return false ;
62+ while (*p == ' *' ) p++;
63+ return *p == ' \0 ' ;
6464}
6565
6666
You can’t perform that action at this time.
0 commit comments