* 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