Can anyone please help me with optimising the solution
code-
checker() is used to generate all the substring possible
map<pair,int> is used to store if (string of i+1,j-1 is. a pallindrome or not)
@ssrivastava990 ,@galencolin, @akshitm16, @ssjgz, @dardev
class Solution {
public:
string ans;
int mx=0,x=0,y=0,n;
int dp[1001][1001];
map<pair<int,int>,int>mp;
void checker(string s,int i,int j)
{
if(i>j || j<0 || i<0 || i>n || j>n)
{
return;
}
if(dp[i][j]!=-1)return;
dp[i][j]=0;
if(dp[i+1][j]==-1) checker(s,i+1,j);
if(j>0) if(dp[i][j-1]==-1) checker(s,i,j-1);
if((s[i]==s[j]&& ((mp[{i+1,j-1}]==1)||(j-i<=1))))
{
if((j-i)>mx){
mx = j-i;
x=i;y=j;
}
mp[{i,j}]=1;
}
}
string longestPalindrome(string s) {
if(s.size()==0)return s;
n = s.size();
memset(dp,-1,sizeof(dp));
for(int i=0;i<n;i++)
{
mp[{i,i}]=1;
}
checker(s,0,s.size()-1);
for(int k=x;k<=y;k++)
ans+=s[k];
return ans;
}
};