forked from lilianweng/LeetcodePython
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregex_match.py
76 lines (63 loc) · 2.1 KB
/
regex_match.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
'''
Leetcode: Regular Expression Matching
'.' 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
http://leetcode.com/2011/09/regular-expression-matching.html
DFA: http://swtch.com/~rsc/regexp/regexp1.html
'''
from __future__ import division
import random
def split_pattern(p):
plist = []
while len(p) > 0:
if len(p) == 1:
plist.append( p[0] )
p = p[1:]
else:
if p[1] == '*':
plist.append( p[:2] )
p = p[2:]
else:
plist.append( p[0] )
p = p[1:]
print 'formatted pattern:', plist
return plist
def is_match(s, plist):
#print s, plist,
if len(plist) == 0: return len(s) == 0
'''if len(s) == 0:
for p in plist:
if not p.endswith('*'): return False
return True'''
p = plist[0]
# p has no '*'
if not p.endswith('*'):
#print 'matching', (s[0] if len(s) > 0 else ''), 'and', p
if p.startswith('.'):
return len(s) > 0 and is_match(s[1:], plist[1:])
else:
return len(s) > 0 and s[0] == p and is_match(s[1:], plist[1:])
# p has '*'
else:
#print 'matching', p
if p.startswith('.'):
return is_match(s, plist[1:]) or (len(s) > 0 and is_match(s[1:], plist))
else:
return is_match(s, plist[1:]) or (len(s) > 0 and s[0] == p[0] and is_match(s[1:], plist))
if __name__ == '__main__':
print is_match('aaaaaabc', split_pattern('aa*bc*.*'))
print is_match('aaaaaabc', split_pattern('aa*b..'))
print is_match('aaaaaa', split_pattern('aaa'))
print is_match('', split_pattern('aaa'))
print is_match('xyz', split_pattern('...'))