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 two new problems in leetcode section palidrome-partitioning an… #138

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
70 changes: 70 additions & 0 deletions Leetcode/Palindrome_Partitioning.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

// Link :- https: // leetcode.com/problems/palindrome-partitioning/

// cpp solution ---

#include <bits/stdc++.h>

using namespace std;

class Solution
{
public:
vector<vector<string>> partition(string s)
{
vector<vector<string>> res;
vector<string> path;
partitionHelper(0, s, path, res);
return res;
}

void partitionHelper(int index, string s, vector<string> &path,
vector<vector<string>> &res)
{
if (index == s.size())
{
res.push_back(path);
return;
}
for (int i = index; i < s.size(); ++i)
{
if (isPalindrome(s, index, i))
{
path.push_back(s.substr(index, i - index + 1));
partitionHelper(i + 1, s, path, res);
path.pop_back();
}
}
}

bool isPalindrome(string s, int start, int end)
{
while (start <= end)
{
if (s[start++] != s[end--])
return false;
}
return true;
}
};
int main()
{
string s = "aabb";
Solution obj;
vector<vector<string>> ans = obj.partition(s);
int n = ans.size();
cout << "The Palindromic partitions are :-" << endl;
cout << " [ ";
for (auto i : ans)
{
cout << "[ ";
for (auto j : i)
{
cout << j << " ";
}
cout << "] ";
}
cout << "]";

return 0;
}
70 changes: 70 additions & 0 deletions Leetcode/Wildcard_Matching.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Link - https : // leetcode.com/problems/wildcard-matching/

// cpp solution ---->

#include <bits/stdc++.h>
using namespace std;

// Function to check if a substring of S1 contains only '*'
bool isAllStars(string &S1, int i)
{
for (int j = 0; j <= i; j++)
{
if (S1[j] != '*')
return false;
}
return true;
}

// Function to check if S1 matches S2 using wildcard pattern matching
bool wildcardMatchingUtil(string &S1, string &S2, int i, int j, vector<vector<bool>> &dp)
{
// Base Cases
if (i < 0 && j < 0)
return true;
if (i < 0 && j >= 0)
return false;
if (j < 0 && i >= 0)
return isAllStars(S1, i);

// If the result for this state has already been calculated, return it
if (dp[i][j] != -1)
return dp[i][j];

// If the characters at the current positions match or S1 has a '?'
if (S1[i] == S2[j] || S1[i] == '?')
return dp[i][j] = wildcardMatchingUtil(S1, S2, i - 1, j - 1, dp);
else
{
if (S1[i] == '*')
// Two options: either '*' represents an empty string or it matches a character in S2
return dp[i][j] = wildcardMatchingUtil(S1, S2, i - 1, j, dp) || wildcardMatchingUtil(S1, S2, i, j - 1, dp);
else
return false;
}
}

// Main function to check if S1 matches S2 using wildcard pattern matching
bool wildcardMatching(string &S1, string &S2)
{
int n = S1.size();
int m = S2.size();

// Create a DP table to memoize results
vector<vector<bool>> dp(n, vector<bool>(m, -1));
return wildcardMatchingUtil(S1, S2, n - 1, m - 1, dp);
}

int main()
{
string S1 = "ab*cd";
string S2 = "abdefcd";

// Call the wildcardMatching function and print the result
if (wildcardMatching(S1, S2))
cout << "String S1 and S2 do match";
else
cout << "String S1 and S2 do not match";

return 0;
}