From 8ed058a35e9747ad2cb131e7be9df679eb6e485f Mon Sep 17 00:00:00 2001 From: Atul-Kashyap Date: Sun, 30 May 2021 22:27:02 +0530 Subject: [PATCH] Added Regular Expression Matching Program in CPP --- Code/C++/regular_expression_matching.cpp | 76 ++++++++++++++++++++++++ string/readme.md | 1 + 2 files changed, 77 insertions(+) create mode 100644 Code/C++/regular_expression_matching.cpp diff --git a/Code/C++/regular_expression_matching.cpp b/Code/C++/regular_expression_matching.cpp new file mode 100644 index 000000000..b2d678247 --- /dev/null +++ b/Code/C++/regular_expression_matching.cpp @@ -0,0 +1,76 @@ +// Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*' where: + +// '.' Matches any single character.​​​​ +// '*' Matches zero or more of the preceding element. + +#include +using namespace std; + + +bool isMatch(string s, string p) { + // finding the length of string and pattern + int m = s.size(), n = p.size(); + // Use dp vector is used to store the intermediate result + vector> dp(m + 1, vector(n + 1, false)); + + dp[0][0] = true; + // When a star(*) is present, we may need to check many different suffixes of the text + // and see if they match the rest of the pattern. + 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]; +} + + +int main() { + + // Input : + string s1 = "aa", p1 = "a"; + cout << isMatch(s1, p1) << endl; + + string s2 = "aa", p2 = "a*"; + cout << isMatch(s2, p2) << endl; + + return 0; +} + + +// Test Cases : + +// Input: s = "aa", p = "a" +// Output: false +// Explanation: "a" does not match the entire string "aa". +// --------------------------------------------------------------- + +// Input: s = "aa", p = "a*" +// Output: true +// Explanation: '*' means zero or more of the preceding element, 'a'. +// Therefore, by repeating 'a' once, it becomes "aa". +// ------------------------------------------------------------------ + +// Input: s = "ab", p = ".*" +// Output: true +// Explanation: ".*" means "zero or more (*) of any character (.)". +// ------------------------------------------------------------------ + +// Input: s = "aab", p = "c*a*b" +// Output: true +// Explanation: c can be repeated 0 times, a can be repeated 1 time. +// Therefore, it matches "aab". +// ------------------------------------------------------------------- + + + +// Time Complexity: O(TP), Let T and P be the lengths of the text and the pattern respectively. + +// Space Complexity: The only memory we use is the O(TP) boolean entries in our cache. +// Hence, the space complexity is O(TP diff --git a/string/readme.md b/string/readme.md index 4705afc3b..a9b242d0f 100644 --- a/string/readme.md +++ b/string/readme.md @@ -28,6 +28,7 @@ A string is a class that contains a char array, but automatically manages it for * Manacher's algorithm (finding the longest palindrome in a string) ----> [Java](/Code/Java/longPalindrome.java) * Maximum occuring character in a string ---->[C++](/Code/C++/max_count.cpp) * Number of deletions to make a string pallindrome ---->[Python](/string/Number_of_deletions_to_make_pallindrome.py) +* Regular Expression Matching ---->[C++](/Code/C++/regular_expression_matching.cpp) * Reverse a string ---->[C++](/Code/C++/reverse_string.cpp) | [Java](/Code/Java/reverse_string.java) * Revere a string using Stack ---->[C++](/Code/C++/reverse_a_string_using_stack.cpp) * Reverse Individual Words in String ----> [Java](/Code/Java/revindivstring.java)