[LeetCode-interview algorithm classic-Java implementation] [010-Regular Expresssion Matching (Regular Expression Matching)],-javaexpresssion

Source: Internet
Author: User

[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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.