Skip to content

Instantly share code, notes, and snippets.

@xiaq
Last active February 16, 2017 22:00
Show Gist options
  • Select an option

  • Save xiaq/6f0a03c46748e5b0a2e32fc56fc401eb to your computer and use it in GitHub Desktop.

Select an option

Save xiaq/6f0a03c46748e5b0a2e32fc56fc401eb to your computer and use it in GitHub Desktop.
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