[Leetcode] [Python] Regular Expression Matching

Source: Internet
Author: User
Tags function prototype

#-*-Coding:utf8-*-
‘‘‘
https://oj.leetcode.com/problems/regular-expression-matching/

Implement regular expression matching with support for '. ' and ' * '.

‘.‘ Matches any single character.
' * ' Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

The function prototype should be:
BOOL IsMatch (const char *s, const char *p)

Some Examples:
IsMatch ("AA", "a") →false
IsMatch ("AA", "AA") →true
IsMatch ("AAA", "AA") →false
IsMatch ("AA", "A *") →true
IsMatch ("AA", ". *") →true
IsMatch ("AB", ". *") →true
IsMatch ("AaB", "C*a*b") →true

===comments by dabay===
I feel so much rubbish about myself. If I did not compress p, I could not get through online judge. is actually a DFA.

When S and P are both empty, the match succeeds.
If S is empty, p is not empty, check the even number of P is not all *.

If S and p are not empty, the first letter
If they do not match,
With no *,false
with *, recursive p[2:]
If it matches,
Without *, recursive s[1:],p[1:]
with *, consider matching 0 to the far end of the case, respectively recursive s[i:],p[2:]
‘‘‘
Class Solution:
# @return A Boolean
def isMatch (self, S, p):
def compress (p):
i = 0
While I < Len (p)-3:
If p[i+1]! = ' * ':
i = i + 1
Continue
If p[i+3] = = ' * ':
If p[i] = = "." or p[i+2] = = ".":
p = p[:i] + ". *" + p[i+4:]
Continue
Elif P[i] = = P[i+2]:
p = p[:i] + p[i+2:]
Continue
i = i + 2
Return P

def ISMATCH2 (S, p):
If Len (s) = = 0:
If Len (p) = = 0:
Return True
If Len (p)% 2 = = 0:
i = 1
While I < Len (p):
If p[i]! = "*":
Return False
i = i + 2
Else
Return True
Else
Return False
If Len (s) > 0 and len (p) = = 0:
Return False

Match_char = p[0]
Multi = False
If Len (P) > 1:
If p[1] = = "*":
Multi = True

If Match_char! = S[0] and Match_char! = ".":
If multi:
Return IsMatch2 (S, p[2:])
Else
Return False

If Multi is False:
Return IsMatch2 (s[1:], p[1:])
Else
result = False
i = 0
result = ISMATCH2 (S, p[2:])
While I < Len (s):
if result = = True:
return result
if (s[i] = = Match_char or Match_char = = "."):
result = ISMATCH2 (s[i+1:], p[2:])
Else
Break
i = i + 1
return result

Return ISMATCH2 (S, Compress (p))


def main ():
s = solution ()
Print S.ismatch ("AA", "A *")


if __name__ = = "__main__":
Import time
Start = Time.clock ()
Main ()
Print "%s sec"% (Time.clock ()-start)

[Leetcode] [Python] Regular Expression Matching

Related Article

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.