Leetcode: Regular Expression Matching
I spent a lot of time digesting this question on leetcode.
The question is as follows:
Matches two strings, s and t.
'.' Can match any character.
'*' Can act as 0 or multiple first characters.
The matching result must overwrite the entire string.
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
For example, in the final example, if c * a * B can match aab, it means that the first * acts as 0 c, and the second * acts as two.
There are two common solutions for this question: recursion and dynamic regulation. The following is a brief analysis of the two methods.
Recursion
One difficulty of this question is how to deal with the symbol "*" in the t string. Sometimes the Code cannot be clearly written at once. We can intuitively think that when "*" appears, this "*" can act as 0, 1, 2... The preceding character also needs to meet the condition (s [I] = p [0]) or (p [I] = '. '& s [I]! = ''). We compare each type of data with the s string. If one type of data can be matched successfully, the data is matched successfully.
If the current matching symbol in the t string is not the symbol "*", it is much easier to do. We directly Pair s [I] = p [0] Or p [0] = '. '. If the match is successful, you can perform the next match.
The reference code is as follows: (refer to others)
// Recursive Method bool isMatch (string s, string p) {if (p. empty () return s. empty (); // p [1] Not * if (p [1]! = '*') {Return (s [0] = p [0] | (p [0] = '.'&&! S. empty () & isMatch (s. substr (1), p. substr (1);} // p [1] is * int I = 0; (; s [I] = p [0] | (p [0] = '. '& I
Dynamic Programming
If you are familiar with dynamic planning, this method may be easier.
We provide a two-dimensional space set f [I] [j] which represents the string s [0 .. I-1] and t [0... J-1] matching successful, we will soon be able to get the Dynamic Programming transfer equation
When t [J-1]! = '*' F [I] [j] = f [I-1] [J-1] & (s [I-1] = t [J-1])
This is easy to understand, s [I-1] and t [J-1] if the same, then its s [0... I-2] to t [0... When the J-2] can still match successfully, then the two strings are matched successfully.
When t [J-1] = '*' t [J-1] can act as 0 or multiple t [J-2], as long as either of the following two conditions is met, f [I] [j] returns true.
T [J-1] act as 0 t [J-2]: f [I] [j] = f [I] [J-2] t [J-1] act as one or more t [J-2]: f [I] [j] = (s [I-1] = p [j-2] | '. '= p [j-2]) & f [I-1] [j]
F [I-1] [j] in the above dynamic equations contains t [J-1] acting as 2, 3... All possibilities of multiple t [J-2], and the results have been computed and saved during the gauge process.
The reference code is as follows: (refer to others)
Bool isMatch (string s, string p) {int m = s. size (), n = p. size (); vector > F (m + 1, vector (N + 1, false); f [0] [0] = true; // The Null String matches with the Null String successfully for (int I = 1; I <= m; I ++) // The result of Null String Matching is false f [I] [0] = false; // p [0 .. j-1] can match null strings and must meet p [j-1] = '*' and p [0 .. j-3] can match with an empty string for (int j = 1; j <= n; j ++) f [0] [j] = j> 1 & '*' = p [j-1] & f [0] [j-2]; for (int I = 1; I <= m; I ++) for (int j = 1; j <= n; j ++) if (p [j-1]! = '*') F [I] [j] = f [I-1] [j-1] & (s [I-1] = p [j-1] | '. '= p [j-1]); else f [I] [j] = f [I] [j-2] | (s [I-1] = p [j-2] | '. '= p [j-2]) & f [I-1] [j]; return f [m] [n];}
The two methods have different ideas. The same part can solve the problem and record it for future review.