"010-regular expresssion Matching (regular expression matching)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
Original Question
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") →false
IsMatch ("AA", "AA") →true
IsMatch ("AAA", "AA") →false
IsMatch ("AA", "A *") →true
IsMatch ("AA", ". *") →true
IsMatch ("AB", ". *") →true
IsMatch ("AaB", "C*a*b") →true
Main Topic
Implements a regular expression matching algorithm that matches any one character, * matches 0 or more leading characters
Thinking of solving problems
Use the tag-matching algorithm to match from back to forward.
Code Implementation
ImportJava.util.Arrays; Public class solution { /** * 010-regular expresssion Matching (regular expression match) * * @param s Match String * @param P-mode string * @return match result, true match, false mismatch */ Public Boolean IsMatch(string S, String p) {//array of markers Boolean[] match =New Boolean[S.length () +1];//InitializeArrays.fill (Match,false);//Assume that the final result is a matchMatch[s.length ()] =true;//The pattern string is processed from the back forward for(inti = P.length ()-1; I >=0; i--) {//If current is * if(P.charat (i) = =' * ') {//Match string to start processing from the last for(intj = s.length ()-1; J >=0; j--) {Match[j] = Match[j] | | match[j +1] && (P.charat (i-1) =='. '|| S.charat (j) = = P.charat (i-1)); } i--; }//If not * Else{ for(intj =0; J < S.length (); J + +) {Match[j] = match[j +1] && (P.charat (i) = ='. '|| P.charat (i) = = S.charat (j)); } match[s.length ()] =false; } }returnmatch[0]; }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/46951847"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java Implementation" "010-regular expresssion Matching (regular expression matching)"