Implement Regular expression matching ‘.‘
with support for and ‘*‘
.
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
Determines whether regular expressions are valid and regular expressions are used. To represent any character with a * to denote one or more previous characters according to these two points in the judgment of the most important thing is to distinguish between the existence or not
The code is as follows:
public class Solution {public Boolean isMatch (string s, String p) { if (p.length () ==0) return s.length () ==0;if (p. Length () ==1) {if (s.length () <1| | S.charat (0)!=p.charat (0) &&p.charat (0)! = '. ') Return False;else{return IsMatch (s.substring (1), p.substring (1));}} if (P.charat (1) = = ' * ') {if (IsMatch (s,p.substring (2))) return true;for (int i=0;i<s.length (); i++) { if (P.charat ( 0)! = S.charat (i) &&p.charat (0)! = '. ') Return False;if (IsMatch (s.substring (i+1), p.substring (2)) return true;}} Else{if (s.length () <1| | (P.charat (0)! = '. ' &&s.charat (0)!=p.charat (0)) return False;else{return IsMatch (s.substring (1), p.substring (1));}} return false; }}
Regular Expression Matching