From b53da9f4a74c812a1676f95529c3ee5f7826dd31 Mon Sep 17 00:00:00 2001 From: Anneshu Nag <132702983+NK-Works@users.noreply.github.com> Date: Sun, 3 Nov 2024 00:23:36 +0530 Subject: [PATCH 1/2] regular-exp-algo --- .../regular-expression-matching.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Algorithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/regular-expression-matching.py diff --git a/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/regular-expression-matching.py b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/regular-expression-matching.py new file mode 100644 index 0000000000..cc72995af7 --- /dev/null +++ b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/regular-expression-matching.py @@ -0,0 +1,24 @@ +def isMatch(s, p): + m, n = len(s), len(p) + + # Initialize a (m+1) x (n+1) DP table + dp = [[False] * (n + 1) for _ in range(m + 1)] + + dp[0][0] = True + + for j in range(1, n + 1): + if p[j - 1] == '*': + dp[0][j] = dp[0][j - 2] + + for i in range(1, m + 1): + for j in range(1, n + 1): + if p[j - 1] == s[i - 1] or p[j - 1] == '.': + dp[i][j] = dp[i - 1][j - 1] + elif p[j - 1] == '*': + dp[i][j] = dp[i][j - 2] or (dp[i - 1][j] and (p[j - 2] == s[i - 1] or p[j - 2] == '.')) + + return dp[m][n] + +s = "aab" +p = "c*a*b" +print(f"Does '{s}' match the pattern '{p}': {isMatch(s, p)}") From 793a44b406d7d79bade60d3e6df379934d7827d4 Mon Sep 17 00:00:00 2001 From: NK-Works Date: Sat, 2 Nov 2024 18:54:25 +0000 Subject: [PATCH 2/2] updating Project-Structure.md --- Project-Structure.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Project-Structure.md b/Project-Structure.md index 040c0437e3..2faaa325d5 100644 --- a/Project-Structure.md +++ b/Project-Structure.md @@ -87,6 +87,8 @@ * [Climbing-Stairs](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/climbing-stairs.py) * [Fibonacci-Seq](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/fibonacci-seq.py) * [House-Robber](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/house-robber.py) + * Hard-Dp-Problems + * [Regular-Expression-Matching](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/regular-expression-matching.py) * Medium-Dp-Problems * [Coin-Change](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Medium-DP-Problems/coin-change.py) * [Longest-Inc-Subseq](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Medium-DP-Problems/longest-inc-subseq.py)