// here is easier to understand. 11/12/2015
// http://shmilyaw-hotmail-com.iteye.com/blog/2154716
// spend long time to understand, still hard to understand. 7/27/2014
bool isMatch(const char *s, const char *p) {
if (*p == '\0' || *s == '\0') return *p == *s;
if (*p == *s || *p == '?') return isMatch(++s,++p);
if (*p == '*'){
while(*p == '*') ++p; // ignore multiple stars
if (*p == '\0') return true;
// now *p is not a star.
while(*s != '\0' && !isMatch(s,p)){
++s;
}
return *s != '\0';
}
return false;
}
// http://www.darrensunny.me/leetcode-wildcard-matching-2/
/*
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
*/
/*
This problem is similar to a previous one LeetCode - Regular Expression Matching.
But a recursive brute-force approach would not make it pass LeetCode OJ due to
the exceeding of the time limit. Alternatively, we can apply dynamic programming
techniques to avoid duplicate computation during the brute-force search.
Specifically, we maintain a table t[i][j] , where t[i][j] is true if p[0...i]
matches s[0...j] . Based on whether p[i] is a wildcard, we can decide t[i][j]
accordingly:
p[i]≠′∗′ : t[i][j] is true if and only if t[i−1][j−1] is true and s[j] and
p[i] match (i.e., p[i]=′?′ or s[j]=p[i] )
p[i]=′∗′ : t[i][j] is true if and only if any of t[i−1][j′] is true, where 0≤j′<j .
Since when calculating each cell in the table, only the elements in the preceding
row is used, the whole table would be unnecessary but an array suffices.
*/