"topic description"
Please implement a function to match the include "." and a regular expression of "*".
The character "." In the pattern. Represents any one character, and "*" means that the preceding character can appear any time (including 0 times).
In this point, a match is a string that matches all the characters of the entire pattern.
For example, the string "AAA" matches the schema "A.A" and "ab*b*ac*a", but does not match "aa.a" and "Ab*a"
Question 1: Here's "." How the match is made, and whether it contains special characters that need to be escaped. "Ideas for solving problems"
When the next character in the pattern is not "*":
If the current character of the string matches the current character in the pattern, both the string pointer and the pattern pointer move one character back and then match the remaining.
Returns false directly if the current character in the string does not match the current character in the pattern.
When the next in the pattern is "*":
If the current character of the string does not match the current character of the pattern, the pattern pointer moves back 2 characters to continue the match.
If the current character of the string matches the current character of the pattern, there are 3 ways to match it:
1, the mode pointer to move 2 characters, equivalent to x* is ignored;
2, the string pointer to move 1 characters, after the pattern pointer moved 2 characters;
3, the string pointer to move 1 characters, the pattern does not change, that is, continue to match the next character, because * can match a number of places;
The point to note here is that in Java, the array is always checked for bounds. "Source Display"
Test examples: S1 = "AAA", s2 = "Ab*b*ac*a"
public class Solution {public static void main (string[] args) {//1. How to convert a string to a character array.
String S1 = "AAA";
char[] Sourcestr = S1.tochararray ();
String s2 = "AA.A";
char[] Patternstr = S2.tochararray ();
Boolean result = new Solution (). Match (Sourcestr, PATTERNSTR);
System.out.println ("string [" + S1 + "," + s2 + "] Match:" + result);
Public boolean match (char[] str, char[] pattern) {if (str = NULL | | pattern = = NULL) {
return false;
int strindex = 0;
int patternindex = 0;
return Matchcore (str, strindex, pattern, patternindex); public boolean matchcore (char[] str, int strindex, char[] pattern, int patternindex) {//validity check: str to tail, patter
N to tail, matching success if (Strindex = str.length && Patternindex = = pattern.length) {return true; //pattern first to tail, matching failure if (strindex!= str.length && patternindex = PatTern.length) {return false;
}//Mode 2nd (next character) is *, and the 1th (current character) of the string matches the pattern 1th (current character) and is divided into 3 matching modes;//If the string 1th (the current character) does not match the mode 1th (current character), the pattern pointer moves back 2 digits if (Patternindex + 1 < pattern.length && Pattern[patternindex + 1] = = ' * ') {if (Strindex!= str
. length && Pattern[patternindex] = = Str[strindex]) | | (Pattern[patternindex] = = '. ' && strindex!= str.length))
{///as pattern match 1 characters return Matchcore (str, strindex, patterns, Patternindex + 2) Treat as a pattern match 1 characters | |
Matchcore (str, strindex + 1, pattern, Patternindex + 2)//* match 1, then match the next in Str ||
Matchcore (str, strindex + 1, pattern, patternindex);
else {return Matchcore (str, strindex, pattern, Patternindex + 2);
}//Mode 2nd is not *, and the string 1th (current character) matches the mode 1th (current), then 1 digits are moved back or false directly if ((Strindex!= str.length && pattern[patternindex] = = Str[strindex]) | | (Pattern[patternindex] = = '. ' && strindex!= str.length))
{return Matchcore (str, strindex + 1, pattern, Patternindex + 1);
return false; }
}
"Summary of related issues"
1. How to convert a string to a character array.
public class Solution {public
static void Main (string[] args) {
string = "AAA";
char[] Chararray = String.tochararray ();
for (int i=0; i<chararray.length; i++) {
System.out.print (Chararray[i] + "");}}}
2. Here, "." How the match is made, and whether it contains special characters that need to be escaped.
by pattern[patternindex] = = '. ' Here's the "." Match any ASCII character