[Leetcode] Wildcard Matching

Source: Internet
Author: User

Analysis: * can match any character, including more than 0 consecutive characters. * is equivalent to 1 *. * If there are no other characters, * p is * and * s is *, we have two options: Skip * p *, that is to say, the * matches 0 characters and continues backward matching.
One is that we need to use * to match multiple characters to complete the matching.

* If there are other characters, find the character that matches the non-* character backward in the s string. If not found, the character does not match. If found, it may be different. It is certainly good to use the first non-* character in s to match the * character in p. Let's look at another situation. 2. The non-* character that is first encountered in S and * in p cannot match it. After the first matching of e in s and e in p, there is a mismatch in the Process of continued matching, but in fact the s string and p match, that is to say, to continue expanding the * scope, match e. However, we found that p and s both run, p points to g, and s points to the Second e. Therefore, you need to record the position of the Non-* characters after the *, so that the matching can be re-performed when the * does not match in the future.
Implementation:

Bool isMatch (const char * s, const char * p) {const char * backtrack_s = NULL; const char * backtrack_p = NULL; while (* s) {// match if (* p = '? '| * S = * p) {++ s; ++ p;} // don't match. else {// meet * if (* p = '*') {// skip multiply *. while (* p = '*') + + p; if (* p = '\ 0') return true; // record the next position *. backtrack_s = s; backtrack_p = p;} // else {// Note: Check whether * if (backtrack_p) appears before. {// note: when an inequality occurs in the current position, return to the next position and compare it again. // its significance is to continue to expand. S = ++ backtrack_s; p = backtrack_p; // restore the position of p} // The position does not match, but does not match. Else return false ;}}// end whilewhile (* p = '*') // process * ++ p at the end of p; return (* s = '\ 0' & * p =' \ 0 ');}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.