MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / Solution

Class Solution

LeetCode_problems/Wildcard_Matching/solution.py:7–31  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5# matrix[0][j] = matrix[0][j-1] if the character p[j-1] is a "*" i.e., text string is null
6
7class Solution:
8 def isMatch(self, s: str, p: str) -> bool:
9 matrix = [[False for x in range(len(p) + 1)] for x in range(len(s) + 1)]
10 matrix[0][0] = True
11
12 for i in range(1,len(matrix[0])):
13 if p[i-1] == '*':
14 matrix[0][i] = matrix[0][i-1]
15
16 for i in range(1, len(matrix)):
17 for j in range(1, len(matrix[0])):
18 # If the characters match, the result is the same as result for lengths minus one. The characters match if:
19 # a) If pattern char is '?' then it matches with any char of text
20 # or b) If the current chars in both match
21 if s[i - 1] == p[j - 1] or p[j - 1] == '?':
22 matrix[i][j] = matrix[i-1][j-1]
23
24 # If '*' is encountered, then we have two choices:
25 # a) We ignore the '*' and move to the next char in the pattern i.e., '*' indicates an empty sequence
26 # b) '*' matches with the ith char in the input string
27 elif p[j-1] == '*':
28 matrix[i][j] = matrix[i][j-1] or matrix[i-1][j]
29 else:
30 matrix[i][j] = False
31 return matrix[len(s)][len(p)]

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected