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:
*The characters in front of it are ignored, because*It can represent the previous characters 0 times.*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;
}
}
}
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 ...
Due to this problem is the array of characters, so was forced to use DP; otherwise if given string, then use a recursive easier. ...
Title description Please implement a function to match regular expressions including ’.’ And ’* ’. The character in the pattern ’.’ Means any character, and ’...
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...
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...
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...
recommend Complete "Sword" Algorithm Analysis Series Click 👉"Sword" full analysis Java version Topic description Implement a function to match the match, including '.' And ''Regul...
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 ...
Problem Description: Implement a function to match regular expressions including ‘.’ and ‘*’. The character ‘.’ in the pattern represents any one character, and &ls...