Leetcode 44. Wildcard Matching (Hard) (cpp)
2016-11-25 01:15
429 查看
Leetcode 44. Wildcard Matching (Hard) (cpp)
Tag: Dynamic Programming, Backtracking, Greedy, String
Difficulty: Hard
/*
44. Wildcard Matching (Hard)
Implement wildcard pattern matching with support for '?' and '*'.
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).
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", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false
*/
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.size(), n = p.size();
vector<vector<bool>> t(m + 1, vector<bool>(n + 1, false));
t[0][0] = true;
for (int j = 1; j <= n; j++) {
t[0][j] = p[j - 1] == '*' && t[0][j - 1];
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (p[j - 1] != '*') {
t[i][j] = t[i - 1][j - 1] && (s[i - 1] == p[j - 1] || p[j - 1] == '?');
}
else {
t[i][j] = t[i][j - 1] || t[i - 1][j];
}
}
}
return t[m]
;
}
};
Tag: Dynamic Programming, Backtracking, Greedy, String
Difficulty: Hard
/*
44. Wildcard Matching (Hard)
Implement wildcard pattern matching with support for '?' and '*'.
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).
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", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false
*/
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.size(), n = p.size();
vector<vector<bool>> t(m + 1, vector<bool>(n + 1, false));
t[0][0] = true;
for (int j = 1; j <= n; j++) {
t[0][j] = p[j - 1] == '*' && t[0][j - 1];
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (p[j - 1] != '*') {
t[i][j] = t[i - 1][j - 1] && (s[i - 1] == p[j - 1] || p[j - 1] == '?');
}
else {
t[i][j] = t[i][j - 1] || t[i - 1][j];
}
}
}
return t[m]
;
}
};
相关文章推荐
- [Leetcode] 44. Wildcard Matching 解题报告
- [Leetcode] 44. Wildcard Matching
- (Java) LeetCode 44. Wildcard Matching —— 通配符匹配
- leetcode 44. Wildcard Matching
- LeetCode 44 Wildcard Matching(字符串匹配问题)
- [LeetCode] 44. Wildcard Matching 外卡匹配
- [Leetcode] 44. Wildcard Matching
- 44. Wildcard Matching Leetcode Python
- 44. Wildcard Matching -- LeetCode
- leetcode 44 Wildcard Matching
- leetcode 10|44. Regular Expression Matching 44. Wildcard Matching
- LeetCode 44 Wildcard Matching
- Leetcode 44. Wildcard Matching
- LeetCode 44. Wildcard Matching(通配符匹配)
- LeetCode------44. Wildcard Matching(?*匹配)
- [leetcode-44]Wildcard Matching(java)
- leetcode:44. Wildcard Matching
- leetcode 44:Wildcard Matching
- leetcode: 44. Wildcard Matching [✗]
- leetCode 44.Wildcard Matching (通配符匹配) 解题思路和方法