Algorithm Cultivation 52, regular expression matching

tags: algorithm  Regular expression  

Description:

Please implement a function to match and include '.'and'*'Regular expression. The characters in the pattern '.'Indicates any character, and'*'It means that the characters in front of it can appear any time (including 0 times). In this question, matching refers to the entire mode of matching all the characters of the string. For example, string "aaa" and mode "a.a"and"ab*ac*a"Matching, but" with "aa.a"and"ab*a"Not matching.

  Solution idea:

This question is not difficult to understand, but there are many situations that need to be considered during the matching process, and you need to consider each situation carefully.

First of all, we analyze how to match a character. When matching the characters in the mode string with a character, if the character in the mode is '.', Then any character can be matched; or if the two characters are the same, then you can then you can Match, then match the next character.

Relatively speaking,When the second character in the mode string is not*hourThe problem is relatively simple: If the first character of the first character and the first character of the pattern string match, the string and mode string pointer move the character backwards, and then match the remaining string and mode. If the first character does not match, you can return False directly.

whenThe second character of the pattern string is*hourThe situation is more complicated, because there may be a variety of different matching methods:

  • Regardless of whether the first character is equal, the pattern is moved backward two characters, which is equivalent to*The characters in front of it are ignored, because*It can represent the previous characters 0 times.
  • If the pattern string is matched with the first character and the first character of the string, the character string moves a character back to the back, compares the next bit, and there are two cases in the mode string at this time: the pattern string moves back to the back two characters, also You can keep the mode unchanged (because because*It can represent the previous characters many times).

As shown in the figure below, when the character that matches the state 2 and the string is A, there are two options: enter the state 3 or keep the state 2.

  Programming Implementation (Java):

public class Solution {
    public boolean match(char[] str, char[] pattern){
        /*
        Idea: Compare the first two characters, recursively comparison
        */
        if(str==null || pattern==null)
            return false;
        return match(str,0,pattern,0);
    }
    public boolean match(char[] str,int i,char[] pattern,int j){
                 if (i == str.Length && J == Pattern.length) // All are empty
            return true;
                 if (i <str.length && j == Pattern.length) // mode string is empty
            return false;
                 // The following j must be <Len
                 if (J+1 <Pattern.Length && Pattern [j+1] == '*') {// The second character is*
                         if ((I <str.length && Str [i] == Pattern [J]) || Condition 
                return match(str,i,pattern,j+2) || match(str,i+1,pattern,j+2) || match(str,i+1,pattern,j);
                                 // The representatives match 0, 1 and multiple 
                         else // The first character does not wait
                return match(str,i,pattern,j+2);
                 } else {// The second character is not*
            if((i<str.length && str[i]==pattern[j]) || ( pattern[j]=='.'&& i< str.length))
                return match(str,i+1,pattern,j+1);
            else
                return false;
        }
        
    }
}

 

Intelligent Recommendation

[ off brush]: Python: 52. Regular expression matching

Regular expression matching Time limit: 1 second Space limit: 32768K Heat index: 171528 Knowledge of this topic: String algorithm knowledge video explanation Description of the topic Please implement ...

[52] wins the Offer, regular expression matching

Due to this problem is the array of characters, so was forced to use DP; otherwise if given string, then use a recursive easier.  ...

Sword refers to offer-52: regular expression matching

Title description Please implement a function to match regular expressions including ’.’ And ’* ’. The character in the pattern ’.’ Means any character, and ’...

[Sword refers to Offer] 52, regular expression matching

This question also uses two-dimensional dp. Each item of the array is initialized to false, but it should be noted that it cannot be written as new Array(m+1).fill(new Array(n+1).fill(false)), because...

More Recommendation

Sword refers to Offer: 52 regular expression matching

1. Flexible use of recursion, especially how to return recursively in various situations 2. The basic nature of the string: char* str cannot be moved backward with str++, it must be str+1 3. The consi...

Sword refers to offer-52. Regular expression matching

Title description Please implement a function to match regular expressions including'.' and'*'. The character'.' in the pattern means any character, and'*' means that the character before it can appea...

"Sword" - 52. Regular expression matching (Java)

recommend Complete "Sword" Algorithm Analysis Series Click 👉"Sword" full analysis Java version Topic description Implement a function to match the match, including '.' And ''Regul...

Algorithm: Regular Expression Matching (regular expression matching)

Description Algorithm: Regular Expression Matching LeetCode address:https://leetcode.com/problems/regular-expression-matching/ topic: Given an input string (s) and a pattern §, implement regular ...

Regular expression matching algorithm

Problem Description: Implement a function to match regular expressions including ‘.’ and ‘*’. The character ‘.’ in the pattern represents any one character, and &ls...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top