Give you a character string s and a law p, invite you to implement a support '' and '*' in the regular expression matching.
'.' Matches any single character
'*' matches zero or more of the preceding element that a
so-called matching, to cover the entire string s, and not part of the string.
Description:
S may be empty, and only lowercase letters az from the.
p may be empty and contain only lowercase letters from az, and characters. and *.
Example 1:
Input:
S = "AA"
P = "A"
Output: false
interpretation: "a" can not match the entire string "aa".
Example 2:
Input:
S = "AA"
P = "A *"
Output: true
explanation: because the '*' matches zero or representatives of the foregoing that a plurality of elements, this is in front of the element 'a'. Thus, the string "aa" may be regarded as 'a' is repeated once.
Example 3:
Input:
S = "ab &"
P = "*."
Output: true
explained: ". *" Denotes zero or more matches ( '*') of any character ( '. ').
Example 4:
Input:
S = "AAB"
P = "C * A * B"
Output: true
Explanation: because the '*' means zero or more, where 'c' is 0, 'a' is repeated once. So it can match the string "aab".
Example 5:
Input:
S = "Mississippi"
P = "MIS * IS * P *."
Output: false
Source: stay button (LeetCode)
Link: https: //leetcode-cn.com/problems/regular-expression-matching
All the copyright from withholding network. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
Get this question, thought, this difficulty is difficult, feeling a bit around. Analysis of a moment, feeling can start a string match from the beginning, the only thing to deal with is the p * and., The idea is that, if matched, s and p are skipping to the next, do not match, that is wrong.
So need to add * If the current match, a next s like the current, the next one is p *, then also suitable. But this is a problem arise, such as s = sssa p = s * ssa, * so I can not tell in the end to where you want to match.
So I think whether it can reverse matching? The results of a moment, or did not solve the problem, such as s = sssa p = sss * a, *, or no way to distinguish how much in the end match.
In this case we found that all these reasons are attributed to the front and rear * has emerged in the same string, it is not what it could transform the first p s * ssa or have become sss * a s * a, is the same merger .
Write test code, submit problems are found, after analysis, suddenly found that there is the most impact analysis, most affect performance, and my this method becomes ineffective rule [ '' matches any single character], there is a combination [. "*" represents matching zero or more ( '*') any character ( '.')], will lead back to the above question in the end I want to match where? Example:
s=aaabcd p=a.*abcd
How to do it, it seems that only in such a way exhaustive. Take the above example, the matching. * When the need is determined. * Does not match any character, a character matches, the same characters are a plurality of matching is successful, success, then this is the matching rule.
I have a solution, see the solution to a problem, the solution has a finite state machine, I feel very good, well understood, and then wrote a so-called brute-force method according to the prompts. P is the matching rule to generate a sequence of characters in a similar pattern of finite state machines, each node defines whether the current match character and can be repeated (Match 0 or infinite times).
If the current node is not repeatable, directly determine if a match skip to the next node does not match the direct return false; if the current node is repeatable, skip to the next node, if this branch returns false, it match the current node once this cycle.
struct CHECKNODE { // current character char C; // Can 0-n times the cycle match BOOL REP; // the next character node CHECKNODE * pNext; CHECKNODE () { C = 0 ; REP = to false ; pNext = nullptr a; } }; BOOL checkm ( const String & S, int sindex, CHECKNODE * pNOW) { IF (sindex == s.size ()) { IF (pnow == nullptr) { return true; } else { if (pnow->rep) { return checkm(s, sindex, pnow->pnext); } else { return false; } } } if (sindex < s.size() && pnow == nullptr) { return false; } if (pnow->rep) { if (!checkm(s, sindex, pnow->pnext)) { if (pnow->c == s[sindex] || pnow->c == '.') { return checkm(s, sindex + 1, pnow); } else { return false; } } else { return true; } } else { if (pnow->c == s[sindex] || pnow->c == '.') { return checkm(s, sindex + 1, pnow->pnext); } else { return false; } } return false; } bool isMatch(string s, string p) { CHECKNODE* pheard = nullptr; CHECKNODE* pnow = nullptr; for (auto& iter : p) { if (pheard == nullptr) { pheard = new CHECKNODE(); pnow = pheard; pnow->c = iter; } else { if (iter != '*') { pnow->pnext = new CHECKNODE(); pnow = pnow->pnext; pnow->c = iter; } else { pnow->rep = true; } } } return checkm(s, 0, pheard); }