Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Regular Expression Matching Program in CPP #2068

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions Code/C++/regular_expression_matching.cpp
Original file line number Diff line number Diff line change
@@ -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 <bits/stdc++.h>
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<vector<bool>> dp(m + 1, vector<bool>(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
1 change: 1 addition & 0 deletions string/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down