[Leetcode] Wildcard Matching

Source: Internet
Author: User

* Match any number of characters,. Match a single character

The key is the case of *, if you encounter an asterisk, then you need to enumerate all cases that match the asterisk, including the case of an empty string

First, recursive solution

1 Booleanismrecursive (String s,string p) {2     if(S.compareto ("") ==0)returnP.compareto ("") ==0;3     if(P.charat (0) = = ' * '){4          for(intI=0;i<s.length (); i++){5             if(IsM (S.substring (i), p))return true;6         }7         return false;8}Else{9         if(P.charat (0) = = '. ' | | (P.charat (0) ==s.charat (0))){Ten             returnIsM (s.substring (1), p.substring (1)); One}Else{ A             return false; -         } -     } the}

Second, non-recursive solution

Booleanismiterate (String s,string p) {if(S.compareto ("") ==0)returnP.compareto ("") ==0; intI=0,j=0; intStart=-1; intSs=0;  while(I<s.length () &&j<p.length ()) {        if(I==s.length ())returnj==p.length (); if(P.charat (j) = = ' * ') {Start=J; SS=i+1; I++; }Else if(P.charat (j) = = '. ' | | P.charat (j) = =S.charat (i)) {i++; J++; }Else if(Start!=-1) {i=SS; J=start+1; }Else{            return false; }    }}

[Leetcode] Wildcard Matching

Related Article

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.