forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create 2002 maximum-product-of-the-length-....cpp
- Loading branch information
1 parent
afb94e9
commit 4f1ef7d
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
cpp/2002-maximum-product-of-the-length-of-two-palindromic-subsequences.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
Approach: | ||
Need to create all the disjoin subsequence and check if they are palindrome. | ||
keep track of maximum product | ||
Time complexity : O(N*N^3) | ||
Space complexity: O(N) | ||
N is length of the string | ||
*/ | ||
|
||
|
||
class Solution { | ||
public: | ||
|
||
int answer = INT_MIN; | ||
|
||
// function to check if the string is a palindrome | ||
bool isPalindrome(string &s){ | ||
int start = 0; | ||
int end = s.length() - 1; | ||
|
||
while(start<end){ | ||
if(s[start]!=s[end]){ | ||
return false; | ||
} | ||
start++; | ||
end--; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
// function to generate all the disjoint subsequence | ||
void generateAll(int idx, string &s1, string &s2, string& s){ | ||
|
||
if(idx >= s.length()) | ||
{ | ||
if(isPalindrome(s1)&&isPalindrome(s2)){ | ||
int l = s1.length()*s2.length(); | ||
answer = max(answer,l); | ||
} | ||
return; | ||
} | ||
|
||
char c = s[idx]; | ||
|
||
/* | ||
we have three options | ||
1. Add the char to the first string | ||
2. Add the char to the second string | ||
3. Add the char to none of the string | ||
*/ | ||
|
||
// add the character in the first string | ||
s1.push_back(c); | ||
generateAll(idx+1,s1,s2,s); | ||
s1.pop_back(); | ||
|
||
// add the character in the second string | ||
s2.push_back(c); | ||
generateAll(idx+1,s1,s2,s); | ||
s2.pop_back(); | ||
|
||
// add character in no string | ||
generateAll(idx+1,s1,s2,s); | ||
} | ||
|
||
int maxProduct(string s) { | ||
|
||
string s1 = ""; | ||
string s2 = ""; | ||
int idx = 0; | ||
|
||
generateAll(idx,s1,s2,s); | ||
|
||
return answer; | ||
} | ||
|
||
|
||
}; |