[Sword refers to Offer learning] [interview question 53: Regular Expression matching], sword refers to offer
Question: implement a function to match regular expressions containing '.' and. The character '.' In the mode indicates any character, while '*' indicates that the character before it can appear any time (including 0 times ). In this question, matching means that all characters in the string match the entire pattern. For example, the string "aaa" matches the pattern "a.a" and "AB * ac * a", but does not match "aa. a" or "AB *.Question Analysis
Each time you extract a character from the string and match the character in the pattern. First, we will analyze how to match a character. If the ch in the mode is '.', it can match any character in the string. If the character ch in the mode is not '.' And the character in the string is ch, they match each other. When the character in the string matches the character in the mode, it then matches the subsequent character.
When the second character in the mode is not '*', the problem is much simpler. If the first character in the string matches the first character in the mode, move one character backward in the string and mode, and then match the remaining string and mode. If the first character in the string does not match the first character in the mode, false is returned directly.
When the second character in the mode is '*', the problem is complicated because there may be multiple matching methods. One choice is to move two characters backward in the mode. This is equivalent to '*' and the character in front of it is ignored, because '*' can match 0 characters in the string. If the first character in the mode matches the first character in the string, move one character backward in the string, and there are two options in the Mode: we can move two characters backward in the mode or keep the mode unchanged.
Code Implementation
Public class Test53 {/*** question: implement a function to match regular expressions containing '.' and. The character '.' In the mode indicates any character, * and '*' indicates that the character before it can appear any time (including 0 times ). In this question, matching means that all characters in the string match the entire pattern. ** @ Param input * @ param pattern * @ return */public static boolean match (String input, String pattern) {if (input = null | pattern = null) {return false;} return matchCore (input, 0, pattern, 0);} private static boolean matchCore (String input, int I, String pattern, int p) {// if (I> = input. length () & p> = pattern. length () {return true;} // only the mode string reaches the end, indicating that the matching fails if (I! = Input. length () & p> = pattern. length () {return false;} // The mode string has not ended, the matching string may end. It may not end. // The next character at the position p is the if (p + 1 <pattern. length () & pattern. charAt (p + 1) = '*') {// The matched string has ended. if (I> = input. length () {return matchCore (input, I, pattern, p + 2);} // The matching string has not ended else {if (pattern. charAt (p) = input. charAt (I) | pattern. charAt (p) = '. ') {return // The matched string moves one position backward, and the pattern string moves two locations backward: matchCore (input, I + 1, pattern, p + 2) // The matching string moves one position backward and the mode string does not move | matchCore (input, I + 1, pattern, p) // The matching string does not move, the mode string moves two positions backward. | matchCore (input, I, pattern, p + 2);} else {return matchCore (input, I, pattern, p + 2) ;}}/// the matched string has ended. if (I> = input. length () {return false;} // The matching string has not ended else {if (input. charAt (I) = pattern. charAt (p) | pattern. charAt (p) = '. ') {return matchCore (input, I + 1, pattern, p + 1) ;}} return false;} public static void main (String [] args) {System. out. println (match ("", "") + "[" + true + "]"); System. out. println (match ("",". * ") +" ["+ false +"] "); System. out. println (match ("",". ") +" ["+ false +"] "); System. out. println (match ("", "c *") + "[" + true + "]"); System. out. println (); System. out. println (match ("",". * ") +" ["+ true +"] "); System. out. println (match ("a", ". ") +" ["+ false +"] "); System. out. println (match ("a", "") + "[" + false + "]"); System. out. println (match ("",". ") +" ["+ true +"] "); System. out. println (match ("a", "AB *") + "[" + true + "]"); System. out. println (match ("a", "AB * a") + "[" + false + "]"); System. out. println (); System. out. println (match ("aa", "aa") + "[" + true + "]"); System. out. println (match ("aa", "a *") + "[" + true + "]"); System. out. println (match ("aa ",". * ") +" ["+ true +"] "); System. out. println (match ("aa ",". ") +" ["+ false +"] "); System. out. println (); System. out. println (match ("AB ",". * ") +" ["+ true +"] "); System. out. println (match ("AB ",". * ") +" ["+ true +"] "); System. out. println (); System. out. println (match ("aaa", "aa *") + "[" + true + "]"); System. out. println (match ("aaa", "aa. a ") +" ["+ false +"] "); System. out. println (match ("aaa", ". a ") +" ["+ true +"] "); System. out. println (match ("aaa ",". a ") +" ["+ false +"] "); System. out. println (match ("aaa", "a * a") + "[" + true + "]"); System. out. println (match ("aaa", "AB * a") + "[" + false + "]"); System. out. println (match ("aaa", "AB * ac * a") + "[" + true + "]"); System. out. println (match ("aaa", "AB * a * c * a") + "[" + true + "]"); System. out. println (match ("aaa ",". * ") +" ["+ true +"] "); System. out. println (); System. out. println (match ("aab", "c * a * B") + "[" + true + "]"); System. out. println (); System. out. println (match ("aaca", "AB * a * c * a") + "[" + true + "]"); System. out. println (match ("aaba", "AB * a * c * a") + "[" + false + "]"); System. out. println (match ("bbbba ",". * a ") +" ["+ true +"] "); System. out. println (match ("bcbbabab ",". * a ") +" ["+ false +"] ") ;}}
Running result
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.