From b86a89f7dc605c3a493656a778a55f47d7f5afc3 Mon Sep 17 00:00:00 2001 From: JeevanSainik789 <115283993+JeevanSainik789@users.noreply.github.com> Date: Sun, 9 Oct 2022 20:35:01 +0530 Subject: [PATCH] Create Regular Expression Matching --- Regular Expression Matching | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Regular Expression Matching diff --git a/Regular Expression Matching b/Regular Expression Matching new file mode 100644 index 0000000..ab6e1f9 --- /dev/null +++ b/Regular Expression Matching @@ -0,0 +1,18 @@ +class Solution { +public: + bool isMatch(string s, string p) { + int m = s.size(), n = p.size(); + vector> dp(m + 1, vector(n + 1, false)); + dp[0][0] = true; + for (int i = 0; i <= m; i++) { + for (int j = 1; j <= n; j++) { + if (p[j - 1] == '*') { + dp[i][j] = dp[i][j - 2] || (i && dp[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == '.')); + } else { + dp[i][j] = i && dp[i - 1][j - 1] && (s[i - 1] == p[j - 1] || p[j - 1] == '.'); + } + } + } + return dp[m][n]; + } +};