Wildcard Matching
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
解题思路:
这道题与http://blog.csdn.net/kangrydotnet/article/details/46624353类似,注意这里的*号与Regular Expression Matching不同。能够採用Regular Expression Matching开发架构,用递归的方法。
可是会出现超时问题:
class Solution { public: bool isMatch(string s, string p) { return matchHelper(s, p, 0, 0); } bool matchHelper(string& s, string& p, int i, int j){ if(p[j]=='