Share my 5 lines C++ backtracking


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

Log in to reply
 

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