Leetcode 44.Wildcard Matching (wildcard match) ideas and methods for solving problems

Source: Internet
Author: User

Wildcard Matching

‘?‘ 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


Train of thought: this difficulty is bigger, this difficulty refers to the difficulty of time complexity, I use recursive write code, correctness is no problem, but time efficiency simply can not endure. The submission was decisive, however.

My code is as follows (for reference only):

public class Solution {public    Boolean isMatch (string s, String p) {    //Boundary condition to determine        if (s.length () = = 0)            return P.length () = = 0;        if (p.length () = = 0)            return false;        If * If            (p.charat (0) = = ' * ') {            int i = 1;            while (I < p.length () && p.charat (i) = = ' * ') {                i++;            }            p = p.substring (i);//Remove the * if it also matches            i = 0;            while (s.length () > 0) {                if (IsMatch (s,p))//because * matches any sequence, p matches any s to                    return true;                s = s.substring (1,s.length ());//string 1 per intercept, try each            }            return IsMatch (s,p);//If none match, then S is empty        }else{//if not * See if the first character of S matches if            (P.charat (0) = = '? ' | | S.charat (0) = = P.charat (0))                return IsMatch (s.substring (1,s.length ()), P.substring (1,p.length ()));            Return false;//mismatch returns false        }}    }
In the forum to see the great God code, unfortunately when the idea did not understand, left to learn it later.

The code is as follows:

public class Solution {public Boolean IsMatch (string s, String p) {/** * This is to see the forum people with the idea of DP realized * I did not realize, with the recursive write although correct,        But tle * This question I also did not thoroughly understand this code, can only write here, later come to study * * int m = s.length (), n = p.length (); int count = 0;//* number for (int i = 0; i < n; i++) {if (P.charat (i) = = ' * ') count++;//statistics * The number of} if (Count==0 && m! = N) return false;//if not * and m!=n, certainly does not match the else if (N-count > m) return false;//if not the number of * >m, then equal to no * or greater than M, mismatch boolean[] match = new boolean[m+1];//default is all false match                [0] = true;//The first set to True for (int i = 0; i < n; i++) {if (P.charat (i) = = ' * ') {//= *, then all data after s corresponds to default match                 for (int j = 0; J < m; J + +) {match[j+1] = Match[j] | | match[j+1]; }} else {//if it is not *, use the last match for (int j = m-1; J >= 0; j--) {match[j+1] = ( P.charat (i) = = '? ' | | P.charat (i) = = S.charat (j)) && Match[j];            } Match[0] = false;    }} return match[m]; }}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode 44.Wildcard Matching (wildcard match) ideas and methods for solving problems

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.