10. Regular Expression Matching

 

class Solution {
public:
    bool isMatch(string s, string p) {
        if(p.empty())
            return s.empty();
        if(p.size() > 1 && p[1] == '*')
            return isMatch(s,p.substr(2)) || (!s.empty() && (s[0] == p[0] || p[0] == '.') && isMatch(s.substr(1),p));
        else
            return !s.empty() && (s[0] == p[0] || p[0] == '.') && isMatch(s.substr(1),p.substr(1));
    }
};

 

44. Wildcard Matching

 

class Solution {
public:
    bool isMatch(string s, string p) {
        int i = 0, j = 0, iStar = -1, jStar = -1;
        while (i < s.size()) {
            if (s[i] == p[j] || p[j] == '?') {
                ++i; ++j;
            }else if (p[j] == '*') {
                iStar = i;
                jStar = j++;
            } else if (iStar >= 0) {
                i = ++iStar;
                j = jStar + 1;
            } else return false;
        }
        while (j < p.size() && p[j] == '*') ++j;
        return j == p.size();
    }
};