First come to the topic:
‘.‘ Matches any single character. ' * ' Matches zero or more of the preceding element. 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", "A *") →trueismatch ("AA", ". *") →trueismatch ("AB", ". *") →true IsMatch ("AaB", "C*a*b") →true
HMM about this problem at the beginning of the first to see the question to me the general idea is easy to come out. Then there is the approximate framework.
The main thing is to divide the situation clearly. About '. ' And the use of ' * ' and several special cases. In the beginning, I was completely unaware that I could use IMatch, and then I was thinking how to match.
The answer is for reference only. 0.0 The main logical problem to distinguish the case is the most important
public class Solution {public Boolean IsMatch (string s, String p) {//easy to think about if (p.length () = =0) {return s.length () ==0; }//special Case if (p.length () ==1) {if (s.length () <1) {return false; } else if ((S.charat (0)!=p.charat (0)) && (P.charat (0)! = '. ')) {return false; } else{return IsMatch (s.substring (1), p.substring (1)); }}//easy to write, hard to think if (P.charat (1)! = ' * ') {if (s.length () <1) { return false; } else if ((P.charat (0)!=s.charat (0)) && (P.charat (0)! = '. ')) {return false; }else{return IsMatch (s.substring (1), p.substring (1)); }}//most Difficult one else{if (IsMatch (s,p.substring (2))) {return true; } inti=0; while (I<s.length () && (S.charat (i) ==p.charat (0) | | P.charat (0) = = '. ')) {if (IsMatch (s.substring (i+1), p.substring (2))} {return true; } i++; } return false; } }}
Leetcode Notes Regular Expression Matching