[LeetCode-interview algorithm classic-Java implementation] [010-Regular Expresssion Matching (Regular Expression Matching)],-javaexpresssion
[010-Regular Expresssion Matching (Regular Expression Matching )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question
Implement regular expression matching with support '. 'and '*'. '. 'matches any single character. '*' Matches zero or more of the preceding element. the matching shocould cover the entire input string (not partial ). the function prototype shocould 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
Theme
Implement a regular expression matching algorithm. match any character. * match 0 or multiple leading characters.
Solutions
Use the tag matching algorithm to perform matching from the back and forward.
Code Implementation
Import java. util. arrays; public class Solution {/*** 010-Regular Expresssion Matching (Regular Expression Matching) ** @ param s Matching string * @ param p pattern string * @ return Matching result, true match, false do not match */public boolean isMatch (String s, String p) {// boolean [] match = new boolean [s. length () + 1]; // initialize Arrays. fill (match, false); // assume that the final result is a match [s. length ()] = true; // process the forward (int I = p. length ()-1; I> = 0; I --) {// if the current value is * if (p. charAt (I) = '*') {// The matching string is processed from the last one for (int j = 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 (int j = 0; j <s. length (); j ++) {match [j] = match [j + 1] & (p. charAt (I) = '. '| p. charAt (I) = s. charAt (j);} match [s. length ()] = false ;}} return match [0] ;}}
Evaluation Result
Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.
Note
Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/46951847]
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.