My Recursion Solution in C++


  • 0
    class Solution {
    public:
     bool isMatch(string s, string p) {
    		if (s == p || (s.empty() && p.empty())) return true;
    		else if (p.length() >= 2 && p[1] == '*') {
    			int si = 0;
    			while (si<s.length() && (p[0] == s[si] || p[0] == '.')) {
    				si++;
    				if (isMatch(s.substr(si, s.length() - si + 1), p.substr(2, p.length() - 2)))
    					return true;
    			}
    			return isMatch(s, p.substr(2, p.length() - 2));
    		}
    		else if (s.empty() || p.empty()) return false;
    		else if (s[0] == p[0] || p[0] == '.') return isMatch(s.substr(1, s.length() - 1), p.substr(1, p.length() - 1));
    		return false;
    	}
    };
    

Log in to reply
 

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