Last active
February 16, 2017 22:00
-
-
Save xiaq/6f0a03c46748e5b0a2e32fc56fc401eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| memo = {} | |
| def isCharMatch(s, p): | |
| return p in ('.', s) | |
| def isMatch(s, p): | |
| if (s, p) not in memo: | |
| memo[(s, p)] = isMatchInner(s, p) | |
| return memo[(s, p)] | |
| def isMatchInner(s, p): | |
| if p == '': | |
| return s == '' | |
| elif len(p) == 1: | |
| return len(s) == 1 and isCharMatch(s, p) | |
| if p[-1] != '*': | |
| return s != '' and isCharMatch(s[-1], p[-1]) and isMatch(s[:-1], p[:-1]) | |
| return (isMatch(s, p[:-2]) or | |
| (s != '' and isCharMatch(s[-1], p[-2]) and isMatch(s[:-1], p))) | |
| class Solution(object): | |
| def isMatch(self, s, p): | |
| return isMatch(s, p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment