C++ 10 lines code for you to memerize


  • 0
    Q
    class Solution {
    public:
        bool isMatch(string s, string p) {
            return isMatch(s, p, 0, 0);
        }
        bool isMatch(string &s, string &p, int i, int j) {
            int sn = s.size(), pn = p.size();
            if(j == pn) return i == sn;
            if(j+1 < pn && p[j+1]=='*') {
                return isMatch(s, p, i, j+2) || ( ((s[i] == p[j]) || p[j]=='.') && (i<sn) && isMatch(s, p, i+1, j) );  //i<sn is a must, otherwise you will not be able to stop. So if i==sn, then we can only call  isMatch(s,p,i,j+2), which will stop at if(j==pn)
            } else {
                return( s[i]==p[j] || p[j]=='.' ) && isMatch(s, p, i+1, j+1);
            }
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.