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(string s, string p) {
        int ssize = s.size();
        int psize = p.size();
        
        int i = 0, j = 0, pi = 0, pj = psize;
        while (i < ssize)
        {
            if (j < psize && (s[i] == p[j] || p[j] == '?'))
            {
                i++;
                j++;
                continue;
            }
            else if (j < psize && p[j] == '*')
            {
                pj = j++;
                pi = i;
                continue;
            }
            else if (pj < psize)
            {
                j = pj + 1;
                i = ++pi;
                continue;
            }
            return false;
        }
        while (j < psize && p[j] == '*')
        {
            j++;
        }
        
        return j == psize;
    }
};


你可能感兴趣的:(Wildcard Matching)