leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching
https://www.cnblogs.com/grandyang/p/4461713.html
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
https://www.cnblogs.com/grandyang/p/4401196.html
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(); } };