forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2002-maximum-product-of-the-length-of-two-palindromic-subsequences.cpp
81 lines (60 loc) · 1.67 KB
/
2002-maximum-product-of-the-length-of-two-palindromic-subsequences.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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;
}
};