Skip to content

Instantly share code, notes, and snippets.

@DataSolveProblems
Created September 17, 2019 23:40
Show Gist options
  • Save DataSolveProblems/3ca5c92525f5243536fa0e7c5c91b8f9 to your computer and use it in GitHub Desktop.
Save DataSolveProblems/3ca5c92525f5243536fa0e7c5c91b8f9 to your computer and use it in GitHub Desktop.
class Solution:
def isMatch(self, s: str, p: str) -> bool:
matched = [[False for _ in range(len(p) + 1)] for _ in range(len(s) + 1)]
matched[0][0] = True
for i in range(len(s) + 1):
for j in range(1, len(p) + 1):
pattern = p[j - 1]
if pattern == '.':
matched[i][j] = (i != 0 and matched[i - 1][j - 1])
elif pattern == '*':
star = p[j - 2]
matched[i][j] = matched[i][j - 2] or (i > 0 and matched[i - 1][j] and
(star == s[i - 1] or star == '.'))
else:
matched[i][j] = (i != 0 and matched[i - 1][j - 1] and s[i - 1] == pattern)
return matched[-1][-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment