Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fef6309

Browse files
Merge pull request vJechsmayr#436 from rajapuranam/patch-2
Create 0010_Regular_Expression_Matching.py
2 parents c55c1d2 + 4f5d048 commit fef6309

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def isMatch(self, s: str, p: str) -> bool:
3+
string, pattern = [], []
4+
string[:0], pattern[:0] = s, p
5+
string.insert(0, 0)
6+
pattern.insert(0, 0)
7+
s, p = len(string), len(pattern)
8+
dp = [[False for _ in range(p)] for __ in range(s)]
9+
dp[0][0] = True
10+
for i in range(p):
11+
if pattern[i] is '*' and dp[0][i-2]: dp[0][i] = True
12+
for i in range(1, s):
13+
for j in range(1, p):
14+
if pattern[j] is string[i] or pattern[j] is '.':
15+
dp[i][j] = dp[i-1][j-1]
16+
elif pattern[j] is '*' and (pattern[j-1] is string[i] or pattern[j-1] is '.'):
17+
dp[i][j] = dp[i][j-2] or dp[i-1][j]
18+
elif pattern[j] is '*' and not (pattern[j-1] is string[i] or pattern[j-1] is '.'):
19+
dp[i][j] = dp[i][j-2]
20+
return dp[s-1][p-1]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /