Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- # @param A : string
- # @param B : string
- # @return an integer
- def checkMatch(self, A, B, a, b):
- # if a <= 0 or b <= 0:
- # self.dp[a][b] = 1 if a == 0 and b == 0 else 0
- # return self.dp[a][b]
- if a == 0 and b == 0:
- return 1 if A[a] == B[b] or B[b] == "*" or B[b] == "?" else 0
- if a == -1 and b == -1:
- return 1
- elif a == -1 or b == -1:
- return 0
- if self.dp[a][b] != -1:
- return self.dp[a][b]
- if A[a] == B[b] or B[b] == "?":
- self.dp[a][b] = self.checkMatch(A, B, a-1, b-1)
- return self.dp[a][b]
- elif B[b] == "*":
- if b == 0:
- return 1
- ans = self.checkMatch(A, B, a-1, b-1) or self.checkMatch(A, B, a-1, b)
- self.dp[a][b] = ans
- return self.dp[a][b]
- elif A[a] != B[b]:
- self.dp[a][b] = 0
- return 0
- def isMatch(self, A, p):
- pattern = [p[0]]
- for x in range(1, len(p)):
- if p[x-1] == p[x] == "*":
- continue
- else:
- pattern.append(p[x])
- pattern = "".join(pattern)
- self.dp = [[-1]*len(pattern)]*len(A)
- return self.checkMatch(A, pattern, len(A)-1, len(pattern)-1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement