Leetcode_10 regular expression matching []

Article Directory:

  • topic
  • A script and notes
  • A script logic
  • note

topic:

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


And a script Note: [when using: 64ms]

class Solution: define a class #
     DEF IsMatch (Self, S: STR, P: STR) -> BOOL: # define a method and parameter, note that the output value of the Boolean type
        p1 = ' ^ ' + the p-+ ' $ ' pre-accession # ^ string matching model and $ 
        IF re.match (p1, S, flags = 0): # If the match is successful, the correct statement, otherwise enter the wrong statement
             return (True) # successful match, True Boolean return value
         the else :
             return (False) # match fails, False Boolean value is returned

I have used the shell to fix this problem: [in a shell mainly use egrep extended regular expressions to match], is to share the link below

https://blog.csdn.net/weixin_43428906/article/details/102676307

 


 A script logic:

  • I find this question a longer time information, there is no exact match was found python3 the shell [grep -w] so-called string, and matching model to carry "r" parameter must match the model expressed as a string, mode can not use the following variables: p1 variable matching pattern: re.match (r'p1 ', s, flags = 0) are not supported
  • Thinking for a long time, I found that you can use the "^" and "$" on the front and rear respectively, with the corresponding intermediate asterisk symbol and period characters can actually achieve a similar effect in the shell of an exact match
  • In python3 asterisk symbol and symbol period consistent with the requirements of the symbolic title, but an exact match can not do it, before and after adding restrictions on the matching pattern can

 

note: 

  • In fact, I have been in their own environment continue to test, you can return the corresponding "false" or "true" according to the title. But still it can not be too egg hurt! It was discovered that the compiler requires the return code is a Boolean value, that is, the subject of the request to the wrong guidelines, or else I would have to go take a bath! Pit father! ! !

 

 

 

 

Guess you like

Origin www.cnblogs.com/mailong/p/12003515.html