bool isMatch(const char *s, const char *p) { if (p[0] == '\0') { if (s[0] == '\0') return true; else return false; } else if (p[1] != '*'){ if (isCharMatch(s[0], p[0])) return isMatch(&s[1], &p[1]); else return false; } else { // p starts with '_*' // repeating 0 times if (isMatch(s, &p[2])) { return true; } int n = strlen(s); int r = 1; // repeating r times while (r <= n) { if (!isCharMatch(s[r-1], p[0])) return false; else if (isMatch(&s[r], &p[2])) return true; r++; } return false; } }
bool isMatch(char* s, char* p) { int startS=0; int curP=0; char xpreChar; char preChar; int plength=strlen(p); bool isCharMatch(char a,char b); for(int i=0;i<strlen(s);i++){ if(curP>=plength) return false; if(isCharMatch(s[i],p[curP])){ if(p[curP]=='.') xpreChar=s[i]; preChar=p[curP]; curP++; }else if(p[curP]=='*'){ if(!isCharMatch(s[i],preChar)){ i--; curP++; } else { if(preChar=='.') xpreChar=s[i]; continue;} }else if(((curP+1)<plength)&&p[curP+1]=='*'){ curP+=2; i--; } else return false; } bool rmark=false; for(int i=curP;i<plength;i++){ if(p[i]=='*') {rmark=true;continue;} else if(p[i]!='*'&&rmark){ if(p[i]==preChar) continue; else if(preChar=='.'&&p[i]==xpreChar) continue; else if(p[i+1]=='*'){ i++;continue; }else return false; } else if(p[i]!='*'&&!rmark) { if(p[i+1]=='*') {i++;continue;} else return false; } } return true; }