class Solution {
public:
string longestPalindrome(string s) {
int n = s.size();
if(n==1)
return s;
int dp[n+1][n+1]; //substring index denoting characters with indices
for(int i=0;i<=n;i++)
dp[0][i] = dp[1][i] = 1; //since substring with length 0 and 1
//will always be a palindrome
int resE = 1, resL = 1; //Ending idx and length of the substring
string res = "";
for(int i=2;i<=n;i++)
for(int j=i;j<=n;j++){
if(dp[i-2][j-1]==1 and s[j-1]==s[j-i])
dp[i][j] = 1, resE = j, resL = i;
else
dp[i][j] = 0;
}
for(int i=resE-resL;i<resE;i++)
res += s[i];
return res;
}
};
In ths code for the ques https://leetcode.com/problems/longest-palindromic-substring/
I could not understand
//
(int i=2;i<=n;i++)
for(int j=i;j<=n;j++){
if(dp[i-2][j-1]==1 and s[j-1]==s[j-i])
dp[i][j] = 1, resE = j, resL = i;
else
dp[i][j] = 0;
}
// part of the code
Please help