Written question 57. Leetcode OJ (44)

Source: Internet
Author: User

This problem is a similar regular expression match, but I found that the difficulty of the problem is more difficult than the previous problem of a few grades. As the title shows: '? ' Can match any one character, and ' * ' can match any string, giving you two strings, the main string, and the pattern string to determine if they can match.

The difficulty of the problem is ' * ' processing, in the end ' * ' to how many strings to match? This problem is not easy to solve, I started the idea is:

1. Encounter S[i] = P[i] | | P[i] = = '? ' in both cases, both of them advance one

2. When encountering s[i]! = P[i], see if there is a ' * ' in front of it, if not, return false directly, if some words are more troublesome ...

3. When encountering the ' * ' in P, find a string after P, find its first subscript in S, continue, if the failure is backtracking, look for the second occurrence of subscript ...

This way of thinking will use the KMP algorithm, and the location of backtracking is too difficult to control, so I decided to give up, I thought for a long time, no ideas, then had to look at the ideas of netizens, in their problem solving, I found that the problem needs to understand one thing!


OK, here's the clearer, look at the code!

Class Solution {Public:bool IsMatch (string s, String p) {/* This question to think clearly, I first thought is to encounter the * number, in S[i~s.s.len] find p[i~p[j] = = '*' || P[J] = = '? '] The first occurrence of the position, so that better judge whether it is necessary to continue to compare, but this undoubtedly introduced the KMP algorithm, think of this I think this approach is not advisable, KMP algorithm is relatively complex, and then introduce it, it increased the difficulty, the problem and the previous did the regular expression somewhat similar,    But this problem is more difficult than that problem, mainly on the analysis of the trouble, this problem uses the practice of circulation, because I feel that if the number is too many, the number of recursive layers too much, efficiency will be dragged down. */Return Match (S.c_str (), P.c_str ());}      BOOL Match (const char* S, const char* p) {const char * back = NULL; Used to mark the position corresponding to the ' * ' in S and P, starting from that position to find backtracking.  const char * sequence = NULL; Mark P where ' * ' appears where while (*s) {if (*p = = ' * ') {//if ' * ' appears in P, then the back record s with its corresponding position (possibly an empty string), sequence the next position of record ' * ', since the next position to find the back = S;sequence = ++p;} else if (*p = = '? ' | | *s = = *p) {//In both cases, the positions of S and P are added 1++s;++p;} else if (sequence) {//description appears ' * ' and does not match in the lookup, then s should point to the next position in the back, p to the next position of ' * ' in the original position s = ++back;p = sequence;} else{//does not appear ' * ' and does not match, then can jump to conclusions, exit!        return false;}}        while (*p = = ' * ') {//P's sequence ' * ' may appear at the end, then we should skip these positions ++p; }//finally indicates true or Falseretu by whether P points to the last positionRN (*p = = ' + ');}; 
The results are as follows:



Written question 57. Leetcode OJ (44)

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.