"Disclaimer: All rights reserved, please indicate the source of the reprint, do not use for commercial purposes. Contact mailbox: [Email protected] "
Topic Link:http://www.nowcoder.com/practice/45327ae22b7b413ea21df13ee7d6429c?rp=3&ru=/ta/coding-interviews& Qru=/ta/coding-interviews/question-ranking
Title Description
Please implement a function to match the include '. ' And the regular expression of ' * '. The characters in the pattern '. ' Represents any character, and ' * ' means that the character preceding it can appear any time (0 times). In the subject, the match refers to all characters of the string that match the entire pattern. For example, the string "AAA" matches the pattern "a.a" and "ab*ac*a", but does not match "aa.a" and "Ab*a"
Ideas
If the pattern string is now '. ', then only the pattern string and the matching string will be moved back one position.
If the character matches now and the next one in the pattern string is ' * ', we need to discuss the situation
1. The matching string moves backwards by 1 bits, and the pattern string skips ' * '
2. The matching string moves backwards 1 bits, the mode string does not move
3. Match string does not move, mode string Skip ' * '
Class Solution{public:bool match (char* str, char* pattern) {if (Str==nullptr | | pattern==nullptr) return False;return Matchcore (Str,pattern);} BOOL Matchcore (char *str,char *pattern) {if (*str== ' *pattern== ' && true;if ') ' Return *str!= ' && * pattern== ' + ') return false;if (* (pattern+1) = = ' * ') {if (*str==*pattern | | (*pattern== '. ' && *str!= ')) Return Matchcore (str+1,pattern+2) | | Matchcore (Str+1,pattern) | | Matchcore (str,pattern+2); Elsereturn Matchcore (str,pattern+2);} if (*str==*pattern | | (*pattern== '. ' && *str!= ')) Return Matchcore (str+1,pattern+1); return false;}};
Copyright NOTICE: This article is the original blogger article, if reproduced, please indicate the source
"Sword of Offer" regular expression matching