Implement regular expression matching with support for ‘.‘
and ‘*‘
.
‘.‘ 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
title :
Match the regular expression, '. ' Can match any one character, ' * ' before must have a character, two combined to indicate that the previous character appears 0 to infinity.
Solution :
Be sure to note that ' * ' must be used in conjunction with the preceding characters.
This article and code references from: http://harrifeng.github.io/algo/leetcode/regular-expression-matching.html
- The first thing to understand is test instructions:
- "A" corresponds to "a", this match does not explain
- Any letter corresponding to ".", which is also regular common
- 0 to more than the same character X, corresponding to "x*", compared to normal, this place more than a prefix x. x represents the same character to take one, such as "Aaaab" corresponds to "a*b"
- "*" There is also an easy way to neglect is that its "greed" to have a limit. For example "AAA" corresponds to "A*a", code logic can not be greedy all the way to the end
- Regular expressions It is very unrealistic to expect a character to be matched by a character. and the "match" problem is very easy to convert to "match a part", the whole match does not match, depends on "the remaining match" situation. It's good to turn a big problem into a smaller problem: recursion
- After determining recursion, using Java to implement this problem, you will encounter a lot of different from the C, because Java control of the character is not as flexible as the C language pointer Charat must be sure that a location exists before it can be used.
- If pattern is a "x*" type, then the pattern is reduced by two two. Otherwise, it is a reduction of one. No matter how you reduce it, make sure there are so many pattern. For example, S.substring (n), where n is the largest and s.length ()
The code is as follows
Public Static BooleanIsMatch (String s, String p) {if(p.length () = = 0) returnS.length () = = 0; //Length = = 1 is the case, which is easy to forget. //as P is subtracted 2 each time, so if original//p is odd and then finally it'll face the length 1 if(p.length () = = 1) return(s.length () = = 1) && (p.charat (0) = = S.charat (0) | | P.charat (0) = = '. '); //next char isn't ' * ': must match current character if(P.charat (1)! = ' * ') { if(s.length () = = 0) return false; Else return(S.charat (0) = = P.charat (0) | | P.charat (0) = = '. ') && IsMatch (s.substring (1), p.substring (1)); }Else{ //Next char is * while(s.length () > 0 && (p.charat (0) = = S.charat (0) | | P.charat (0) = = '. ')) { if(IsMatch (S, p.substring (2))) return true; S= s.substring (1); } returnIsMatch (S, p.substring (2)); } }
Reference:http://www.cnblogs.com/springfor/p/3893593.html
**regular Expression Match