JZ52 正则表达式匹配
本文最后更新于:2022年4月9日 中午
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| class Solution { public:
bool match(string str, string pattern) { return isMatch(str, 0, pattern, 0); } bool isMatch(const string& s, int i, const string& p, int j) { if (j == p.size()) return i == s.size(); bool first_match = (s.size() > i) && (p[j] == s[i] || p[j] == '.'); if (p.size() >= j + 2 && p[j+1] == '*') { return isMatch(s, i, p, j+2) || (first_match && isMatch(s, i+1, p, j)); } return first_match && isMatch(s, i+1, p, j+1); } };
|