diff --git a/Python/10. Regular_Expression_Matching.py b/Python/10. Regular_Expression_Matching.py new file mode 100644 index 0000000..bc711f3 --- /dev/null +++ b/Python/10. Regular_Expression_Matching.py @@ -0,0 +1,26 @@ +""" +10. Regular Expression Matching + +Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where: + + '.' Matches any single character. + '*' Matches zero or more of the preceding element. + +The matching should cover the entire input string (not partial). +""" + +class Solution(object): + def isMatch(self, s, p): + prev = [False, True] + for j in range(len(p)): + prev.append(p[j]=='*' and prev[j]) + + for i in range(len(s)): + curr = [False, False] + for j in range(len(p)): + if p[j]=='*': + curr.append(curr[j] or curr[j+1] or (prev[j+2] and p[j-1] in (s[i], '.'))) + else: + curr.append(prev[j+1] and p[j] in (s[i], '.')) + prev = curr + return prev[-1] \ No newline at end of file