-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegularExpressionMatching.py
More file actions
66 lines (62 loc) · 2.47 KB
/
Copy pathRegularExpressionMatching.py
File metadata and controls
66 lines (62 loc) · 2.47 KB
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
"""
@Project: leetcode
@file: RegularExpressionMatching.py
@author: AC
@time: 2016/3/27 0:19
@Description: Implement regular expression matching with support for '.' and '*'.
"""
class Solution(object):
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
m = len(s)
n = len(p)
# Init dp
dp = [[False for i in range(n + 1)] for i in range(m + 1)]
# When string and pattern are all None
dp[m][n] = True
# When the string is None, pattern like "a*" can still match it
for i in range(n - 1, -1, -1):
if p[i] == "*":
dp[m][i] = dp[m][i + 1]
elif i + 1 < n and p[i + 1] == "*":
dp[m][i] = dp[m][i + 1]
else:
dp[m][i] = False
for i in range(m - 1, -1, -1):
for j in range(n - 1, -1, -1):
# When the current character is "*"
if p[j] == "*":
if j - 1 >= 0 and p[j - 1] != "*":
dp[i][j] = dp[i][j + 1]
# If the pattern is starting with "*" or has "**" in it
else:
return False
# When the the second character of pattern is "*"
elif j + 1 < n and p[j + 1] == "*":
# When the current character matches, there are three possible situation
# 1. ".*" matches nothing
# 2. "c*" matches more than one character
# 3. "c*" just matches one character
if s[i] == p[j] or p[j] == ".":
dp[i][j] = dp[i][j + 2] or dp[i + 1][j] or dp[i + 1][j + 2]
# Ignore the first two characters("c*") in pattern since they cannot match
# the current character in string
else:
dp[i][j] = dp[i][j + 2]
else:
# When the current character is matched
if s[i] == p[j] or p[j] == ".":
dp[i][j] = dp[i + 1][j + 1]
else:
dp[i][j] = False
return dp[0][0]
if __name__ == '__main__':
s = Solution()
x = ['aa', 'aa', 'aaa', 'aa', 'aa', 'ab', 'aab', 'abcd', 'aaa', 'aa', 'abcd']
y = ['a', 'aa', 'aa', 'a*', '.*', '.*', 'c*a*b', 'd*', 'a*a', 'a*.*', '.*']
for i in xrange(len(x)):
print s.isMatch(x[i], y[i])