[LeetCode]Wildcard Matching

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

思考:又一道正则表达式匹配。参考[这里]。
class Solution {
public:
    bool isMatch(const char *s, const char *p) {
        bool star = false;
        const char* starPtr = NULL;
        const char* savePtr = NULL;
        while(*s != '')
        {
            if(*p == '?') s++, p++;
            else if(*p == '*')
            {
                while(*p == '*') p++;
                p--;
                starPtr = p;
                p++;
                savePtr = s;
                star = true;
            }
            else 
            {
                if(*s == *p) s++, p++;
                else
                {
                    if(star == true) s = ++savePtr, p = starPtr+1;//depend on star
                    else return false;
                }
            }
        }
        while(*p == '*') p++;
        return (*s == '' && *p == '');
    }
};

  

原文地址:https://www.cnblogs.com/Rosanna/p/3481849.html