Regular Expression Matching
2014-07-21 14:52
197 查看
Implement regular expression matching with support for '.' and '*'.
如果P[j+1]!='*',S[i] == P[j]=>匹配下一位(i+1, j+1),S[i]!=P[j]=>匹配失败;
如果P[j+1]=='*',S[i]==P[j]=>匹配下一位(i+1, j+2)或者(i, j+2),S[i]!=P[j]=>匹配下一位(i,j+2)。
'.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true
如果P[j+1]!='*',S[i] == P[j]=>匹配下一位(i+1, j+1),S[i]!=P[j]=>匹配失败;
如果P[j+1]=='*',S[i]==P[j]=>匹配下一位(i+1, j+2)或者(i, j+2),S[i]!=P[j]=>匹配下一位(i,j+2)。
bool isMatch(const char *s, const char *p) { if (*p == '\0') return *s == '\0'; if (*(p + 1) == '*') { while (*s != '\0' && (*s == *p || *p == '.')) { if (isMatch(s, p + 2)) { return true; } s++; } return isMatch(s, p + 2); } else if (*s != '\0' && (*s == *p || *p == '.')) { return isMatch(s + 1, p + 1); } return false; }
相关文章推荐
- LeetCode10 Regular Expression Matching
- Leetcode: Regular Expression Matching
- leetcode----Regular Expression Matching
- LeetCode算法题目: Regular Expression Matching
- 算法设计与分析(7)-- Regular Expression Matching(难度:hard)
- [置顶] 通配符匹配(Wildcard Matching)与正则表达式匹配(Regular Expression Matching)解法解析
- [leetcode] 10 Regular Expression Matching
- LeetCode-Regular Expression Matching
- leetcode习题解答:10. Regular Expression Matching
- LeetCode problem 10: Regular Expression Matching
- 算法第12周Regular Expression Matching[hard]
- leetcode10 Regular Expression Matching
- [leetcode]Regular Expression Matching(正则表达式的匹配)
- LeetCode | Regular Expression Matching
- Leetcode: Regular Expression Matching
- [LeetCode]Regular Expression Matching
- leetcode-Regular Expression Matching
- LeetCode : Regular Expression Matching
- Regular Expression Matching
- Regular Expression Matching leetcode java