"Sword of Offer" regular expression matching

Source: Internet
Author: User

"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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.