diff --git a/Dynamic_Programming/Regular_Expression_Matching.cpp b/Dynamic_Programming/Regular_Expression_Matching.cpp new file mode 100644 index 00000000..dd08e0ff --- /dev/null +++ b/Dynamic_Programming/Regular_Expression_Matching.cpp @@ -0,0 +1,45 @@ +//Given a string s and a pattern p, implement regular expression matching with support for '.' (matches any single character) and '*' (zero or more of the preceding element). +//Example: + +//Input: s = "mississippi", p = "mis*is*p*." +//Output: false + +#include +#include + +using namespace std; + +bool isMatch(string s, string p) { + int m = s.size(); + int n = p.size(); + vector> dp(m + 1, vector(n + 1, false)); + dp[0][0] = true; + + // Fill the first row + for (int j = 1; j <= n; ++j) { + if (p[j - 1] == '*') { + dp[0][j] = dp[0][j - 2]; + } + } + + // Fill the dp array using dynamic programming + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + if (p[j - 1] == s[i - 1] || p[j - 1] == '.') { + dp[i][j] = dp[i - 1][j - 1]; + } else if (p[j - 1] == '*') { + dp[i][j] = dp[i][j - 2] || (dp[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == '.')); + } + } + } + + return dp[m][n]; +} + +int main() { + string s = "mississippi"; + string p = "mis*is*p*."; + bool result = isMatch(s, p); + cout << "Matching result: " << (result ? "true" : "false") << endl; + return 0; +}