GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/010_Regular_Expression_Matching.py at master · terryicoder/leetcode

Expand file tree

/

010_Regular_Expression_Matching.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
36 lines (32 loc) · 1.06 KB
/

010_Regular_Expression_Matching.py

Copy path

File metadata and controls

36 lines (32 loc) · 1.06 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution(object):
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
# bottom up o(m*n)
# https://leetcode.com/discuss/93024/easy-dp-java-solution-with-detailed-explanation
if s == p:
return True
m, n = len(s), len(p)
dp = [[False] * (n + 1) for _ in range(m + 1)]
dp[0][0] = True
for j in range(1, n):
if p[j] == '*' and dp[0][j - 1]:
dp[0][j + 1] = True
# print dp
for i in range(m):
for j in range(n):
if p[j] == '.' or p[j] == s[i]:
dp[i + 1][j + 1] = dp[i][j]
elif p[j] == '*':
if p[j - 1] != s[i] and p[j - 1] != '.':
dp[i + 1][j + 1] = dp[i + 1][j - 1]
else:
dp[i + 1][j + 1] = dp[i + 1][j] or dp[i][j + 1] or dp[i + 1][j - 1]
return dp[m][n]
if __name__ == '__main__':
# begin
s = Solution()
print s.isMatch("", ".*")
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.