1 Topics:
Implement regular expression matching with support for ‘.‘
and ‘*‘
.
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
2 Ideas:
OK, this question I started a comparison really kneeling, no recursion, consider various situations, various if-else, found still can't consider all the situation. See what others write, use recursion to write, the idea is very clear.
Https://leetcode.com/discuss/32424/clean-java-solution
Look at that C + +, want to turn Java, really kneeling, C + + with ' \ ' to represent the end of the string, and Java string is an array, without this one said, various cross-border plus timeout.
Https://leetcode.com/discuss/9405/the-shortest-ac-code
3 Code:
Public BooleanIsMatch (String s, String p) {if(P.isempty ()) {returnS.isempty (); } if(p.length () = = 1 | | P.charat (1)! = ' * ') { if(S.isempty () | | (P.charat (0)! = '. ' && p.charat (0)! = S.charat (0))) { return false; } Else { returnIsMatch (s.substring (1), p.substring (1)); } } //p.length () >=2 && p.charat (1) = = ' * '//The first char is match while(!s.isempty () && (S.charat (0) = = P.charat (0) | | P.charat (0) = = '. ')) { if(IsMatch (S, p.substring (2))) { return true; } //See if the next char of S is still matchs = s.substring (1); } //First char not match, make * to 0 returnIsMatch (S, p.substring (2)); }
[Eetcode 10] Regular Expression Matching