Unable to debug my code


#1

int Solution::solve(string A) {
int n = A.length();
vector<vector>dp(n,vector(n,0));
int maxLen = 1;
for(int i =0;i<n;i++){
dp[i][i]=1;
if(i+1<n && A[i]==A[i+1]){
dp[i][i+1]=2;
maxLen = 2;

    }
}
for(int k =2;k<n;k++){
    for(int i =0;i<n-k;i++){
        int j = i+k;
    
       if(A[i]==A[j])dp[i][j] = 2 + dp[i+1][j-1];
        else dp[i][j] = max(dp[i][j-1],dp[i+1][j]);
        maxLen = dp[i][j];
    
    }
}
return dp[0][n-1];

}


#2

After debugging your code will be like this

int Solution::solve(string A) {
int n = A.length();
vector<vector >dp(n,vector(n,0));
int maxLen = 1;
for(int i =0;i<n;i++){
dp[i][i]=1;
if(i+1<n && A[i]==A[i+1]){
dp[i][i+1]=2;
maxLen = 2;
}else if(i+1 < n){
dp[i][i+1] = 1;
}
}
for(int k =2;k<n;k++){
for(int i =0;i<n-k;i++){
int j = i+k;

       if(A[i]==A[j])dp[i][j] = 2 + dp[i+1][j-1];
        else dp[i][j] = max(dp[i][j-1],dp[i+1][j]);
        maxLen = max(maxLen, dp[i][j]);
    
    }
}
return dp[0][n-1];

}