From 89d12dfe99d51f7df983ddbc6b0c93e1130fc47b Mon Sep 17 00:00:00 2001 From: Kosuri L Indu <118645569+kosuri-indu@users.noreply.github.com> Date: Mon, 16 Oct 2023 00:57:47 +0530 Subject: [PATCH] [Add] : Wildcard Matching program under DYNAMIC PROGRAMMING (#10403) * To add wildcard_matching.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * changes for doctest errors * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * code changes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- dynamic_programming/wildcard_matching.py | 62 ++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 dynamic_programming/wildcard_matching.py diff --git a/dynamic_programming/wildcard_matching.py b/dynamic_programming/wildcard_matching.py new file mode 100644 index 000000000..4ffc4b5d4 --- /dev/null +++ b/dynamic_programming/wildcard_matching.py @@ -0,0 +1,62 @@ +""" +Given two strings, an input string and a pattern, +this program checks if the input string matches the pattern. + +Example : +input_string = "baaabab" +pattern = "*****ba*****ab" +Output: True + +This problem can be solved using the concept of "DYNAMIC PROGRAMMING". + +We create a 2D boolean matrix, where each entry match_matrix[i][j] is True +if the first i characters in input_string match the first j characters +of pattern. We initialize the first row and first column based on specific +rules, then fill up the rest of the matrix using a bottom-up dynamic +programming approach. + +The amount of match that will be determined is equal to match_matrix[n][m] +where n and m are lengths of the input_string and pattern respectively. + +""" + + +def is_pattern_match(input_string: str, pattern: str) -> bool: + """ + >>> is_pattern_match('baaabab','*****ba*****ba') + False + >>> is_pattern_match('baaabab','*****ba*****ab') + True + >>> is_pattern_match('aa','*') + True + """ + + input_length = len(input_string) + pattern_length = len(pattern) + + match_matrix = [[False] * (pattern_length + 1) for _ in range(input_length + 1)] + + match_matrix[0][0] = True + + 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 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] + else: + match_matrix[i][j] = False + + return match_matrix[input_length][pattern_length] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + print(f"{is_pattern_match('baaabab','*****ba*****ab')}")