forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximize Palindrome Length From Subsequences.cpp
89 lines (69 loc) · 2.17 KB
/
Maximize Palindrome Length From 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
82
83
84
85
86
87
88
89
// Runtime: 319 ms (Top 38.38%) | Memory: 70.9 MB (Top 7.75%)
class Solution {
public:
//use to memoize recursive solution
vector<vector<int>> v;
/*
boundary of both strings,
l is length of concatenated string,
ans is used to store final result
*/
int boundary,l,ans;
//Longest palindromic string
int find(string &s,int i,int j)
{
// i->left, j->right
//if left index is greater than right then return
if(i>j) return 0;
//if already calculated, return result
if(v[i][j]!=-1) return v[i][j];
//if we are at stage where i==j,
//means here single character
//count it and increase left boundary and decrease right boundary
if(i==j)
{
return v[i][j]=1+find(s,i+1,j-1);
}
//if two characters are same, count these two and
//increase left boundary and decrease right boundary
//and call function
if(s[i]==s[j])
{
int k=v[i][j]=2+find(s,i+1,j-1);
/*
if left character is from word1 and
right character is from word2
then store maximum result in ans
NOTE: till (boundary-1) index s stored word1
and from index (boundary) s stored word2
*/
if(i<boundary && j>=boundary)
{
ans=max(ans,k);
}
return k;
}
//if characters are not same,
//try both increase left index and make right index constent
// or make left constant or decrease right index
// and return max of both
else
{
return v[i][j]=max(find(s,i+1,j),find(s,i,j-1));
}
}
int longestPalindrome(string word1, string word2) {
//store boundary of strings
boundary=word1.size();
//concatenate both strings
word1+=word2;
//find total length of string
l=word1.length();
//used to memoize solution
v=vector<vector<int>> (l+1,vector<int> (l+1,-1));
//call function
int k=find(word1,0,l-1);
//return final ans
return ans;
}
};