problem:
Implement wildcard pattern matching with support for '?' and '*'.
'?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "*") → true isMatch("aa", "a*") → true isMatch("ab", "?*") → true isMatch("aab", "c*a*b") → false 针对s进行遍历,遇到p中的 * 那么我们将p中的位置pstar记录下来,pstar表示p中 * 的位置,pstar只有在遇到下一个 * 才会进行更新。同时记录s中的位置为starmatch,starmatch表示 p 中 * 匹配s中最后一个字符的位置,starmatch经常要进行更新。需要注意的是当有pstar存在时,遇到不匹配的情况时候一定要更新starmatch。class Solution { public: bool isMatch(string s, string p) { bool result; int ptr_s = 0; int ptr_p = 0; int starmatch = -1; int pstar = -1; while(ptr_s < s.length()) { if(s[ptr_s] == p[ptr_p] || p[ptr_p] == '?') { ptr_s++; ptr_p++; } else if(p[ptr_p] == '*') { starmatch = ptr_s; pstar = ++ptr_p; } else if(pstar > -1) { ptr_p = pstar; ptr_s = ++starmatch; } else return false; } while(p[ptr_p]=='*') ptr_p++; return p[ptr_p]=='\0'; } };
