Description
(Http://www.leetcode.com/onlinejudge penultimate)
Implement wildcard pattern matching with support'?'
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") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "*") → trueisMatch("aa", "a*") → trueisMatch("ab", "?*") → trueisMatch("aab", "c*a*b") → false
The non-recursive algorithm of this question was met by Netease's interviewer. There are still many things to consider for non-recursive algorithms that require writing programs. If you have never seen an algorithm, it is really hard to write it out. Here we provide recursive and non-recursive algorithms. For reference only
Question Analysis:
The matching with a regular expression is a bit different. Here, the * sign is not dependent on the previous symbol and is a wildcard. * Is a multi-wildcard, while * is a single wildcard.
Recursive Solution
In fact, the question is easy to write a recursive method:
The recursive method first checks whether the two input strings are empty. If it is null, the system returns true if the matching is completed.
If the matched string is null and the pattern string is *, the value is true.
If the mode string is null, the result is false.
Class solution {public: bool ismatch (const char * s, const char * P) {If (* s = 0 & * P = 0) {// whether the two strings are empty. If the strings are empty, the system returns true if the matching is completed. Return true;} If (* s = 0) {// If the matched string is null, if all the mode strings are *, truewhile (* P = '*') P ++; return! (* P) ;}if (* P = 0) {return false ;}int I; for (I = 0; s [I] & P [I]; I ++) {// matching process if (s [I]! = P [I]) {If (P [I] = '? ') {// Single wildcard continue;} else if (P [I] =' * ') {// multiple wildcard S + = I; P + = I; return ismatch (S, P + 1) | ismatch (S + 1, P); // matches and does not match, in either case} else {return false ;}}} return ismatch (S + I, P + I); // match the remaining }};
Non-recursive methods the main idea of non-recursive methods is to match from the matching string and the start of the pattern string. If no match is found, check whether a match exists before *, if * exists, the pattern string skips the unmatched characters and the asterisk (*) is skipped. For example, abbc A * C *** startloopi = 0 for the first time; A and A are matched successfully. I = 1; B and * are not found to be matched. The program streams to case '*', because * and B can match, P and S are both + I, star is marked as true, and the matching starts again. The second time startloopi = 0; B and C cannot match to jump to starcheck to see if there is *. If there is *, S + 1, P does not increase (in this case, P is the next position of the previous *), and then matches the third startloop: I = 0; C = C can match at this time s [I] = 0 jump out of the loop {
while(p[i] == '*')i++;return !(p[i]);
}
It is used to check whether all P strings are * after s string matches. If all are *, it can be matched. Otherwise, the match fails.
Bool ismatch (const char * s, const char * P) {int I; bool star = false; startloop: {for (I = 0; s [I]; I ++) {Switch (P [I]) {Case '? ': Break; Case' * ': // if the current value is *, match the character that cannot be matched, and move P to the first character star = true after *. // P points to either the start or the first position P + = I after; S + = I; while (* P = '*') {+ + P;} // check if p is followed by * If (! (* P) return true; goto startloop; // start the loop again. P points to the first non-* default: If (s [I] After *. = P [I]) {// If s [I]! = P [I] goto starcheck ;}}} while (P [I] = '*') I ++; return! (P [I]); starcheck: {If (STAR) {// when s [I]! = P [I], check whether P is * Before p. If it is *, then we move s backward, which is actually enumerating s to s [I], match with P [0. S ++; goto startloop;} return false ;}}
Algorithm is quite fast. In the first case of TLE, algorithm 2 only needs 80 ms. It seems that the non-recursive efficiency is indeed very high.