Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
code changes
  • Loading branch information
kosuri-indu committed Oct 15, 2023
commit a7557bfbdcf5add423b4ce61323d20a4b9f6de5b
15 changes: 4 additions & 11 deletions dynamic_programming/wildcard_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

"""


def is_pattern_match(input_string: str, pattern: str) -> bool:
def is_pattern_match(input_string : str, pattern : str) -> bool:
"""
>>> is_pattern_match('baaabab','*****ba*****ba')
False
Expand All @@ -30,26 +29,21 @@ def is_pattern_match(input_string: str, pattern: str) -> bool:
>>> is_pattern_match('aa','*')
True
"""

input_length = len(input_string)
pattern_length = len(pattern)

match_matrix = [
[False for i in range(pattern_length + 1)] for j in range(input_length + 1)
]
match_matrix = [[False] * (pattern_length + 1) for _ in range(input_length + 1)]

match_matrix[0][0] = True

for i in range(1, input_length + 1):
match_matrix[i][0] = False

for j in range(1, pattern_length + 1):
if pattern[j - 1] == "*":
match_matrix[0][j] = match_matrix[0][j - 1]

for i in range(1, input_length + 1):
for j in range(1, pattern_length + 1):
if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == "?":
if pattern[j - 1] in ("?", input_string[i - 1]):
match_matrix[i][j] = match_matrix[i - 1][j - 1]
elif pattern[j - 1] == "*":
match_matrix[i][j] = match_matrix[i - 1][j] or match_matrix[i][j - 1]
Expand All @@ -58,7 +52,6 @@ def is_pattern_match(input_string: str, pattern: str) -> bool:

return match_matrix[input_length][pattern_length]


if __name__ == "__main__":
import doctest

Expand Down