Question - https://leetcode.com/problems/longest-palindromic-substring/
failing for input - "aaaaa"
I am maintaining a dp array where i have set to 1 for string of size one and for string of size 2 whose left and right character are same, Rest I am setting it to one in case of s[i]==s[j] && dp[i+1][j-1] ==1. Still I am not sure why the longest value returned is of length 3
class Solution {
public:
string longestPalindrome(string s) {
int n = s.size();
int dp[n][n];
int fini=0,finj=0;
for(int i=0; i<s.size(); i++)
for(int j=0; j<s.size(); j++){
if(i==j)
dp[i][j]=1;
else
dp[i][j]=0;
}
for(int i=0; i<s.size()-1; i++){
if(s[i]==s[i+1]){
dp[i][i+1]=1;
fini = i;
finj = i+1;
}
}
//cout<<dp[1][6];
cout<<fini<<finj<<endl;
for(int i=0; i<s.size()-1; i++){
for(int j=i+1; j<s.size(); j++){
cout<<"dp"<<" "<<i<<" "<<j<<' '<<dp[i][j]<<" "<<dp[i+1][j-1]<<endl;
if(s[i]==s[j] && dp[i+1][j-1]){
dp[i][j]=1;
if(abs(j-i+1)>abs(finj-fini+1)){
fini=i;
finj=j;
}
cout<<fini<<" "<<finj<<endl;
}
}
}
return s.substr(fini, finj-fini+1);
}
};