diff --git a/isMatch.py b/isMatch.py new file mode 100644 index 0000000..d12fe49 --- /dev/null +++ b/isMatch.py @@ -0,0 +1,18 @@ +class Solution(object): + def isMatch(self, text, pattern): + """ + :type text: str + :type pattern: str + :rtype: bool + """ + if not pattern: + return not text + + first_match = bool(text) and pattern[0] in {text[0], '.'} + + if len(pattern) >= 2 and pattern[1] == '*': + return (self.isMatch(text, pattern[2:]) or + first_match and self.isMatch(text[1:], pattern)) + else: + return first_match and self.isMatch(text[1:], pattern[1:]) +