leetcode---regular-expression-matching---字符串
2017-11-20 19:56
387 查看
Implement regular expression matching with support for’.’and’*’.
‘.’ 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
‘.’ 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
class Solution { public: bool isMatch(const char *s, const char *p) { int n = strlen(s); int m = strlen(p); vector<vector<bool> > dp(n+1, vector<bool>(m+1, false)); dp[0][0] = true; for(int i=0; i<=n; i++) { for(int j=1; j<=m; j++) { if(j>1 && p[j-1] == '*') dp[i][j] = dp[i][j-2] || (i>0 && (s[i-1] == p[j-2] || p[j-2] == '.') && dp[i-1][j]); else dp[i][j] = i>0 && dp[i-1][j-1] && (s[i-1] == p[j-1] || p[j-1] == '.'); } } return dp [m]; } };
相关文章推荐
- [leetcode]Regular Expression Matching(正则表达式的匹配)
- LeetCode Regular Expression Matching
- leetcode第10题——***Regular Expression Matching
- [Leetcode] 10. Regular Expression Matching
- LeetCode 10. Regular Expression Matching
- LeetCode 10_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 [leetcode]
- 第十四周:[LeetCode]10. Regular Expression Matching
- 《leetCode》:Regular Expression Matching
- LeetCode Problem10 Regular Expression Matching
- LeetCode 010-Regular Expression Matching
- 【leetCode】Regular Expression Matching
- LeetCode – Refresh – Regular Expression Matching
- LeetCode Regular Expression Matching
- leetcode 10. Regular Expression Matching