Just for better understanding of memoization you can see this solution (C++)


#1

int solve(string A,int i,int j,vector<vector> &dp){
if(i==0 || j==0){
return dp[i][j]=0;
}
if(dp[i][j]!=-1){
return dp[i][j];
}
if(A[i-1]==A[j-1] && i!=j){
return dp[i][j] = 1+solve(A,i-1,j-1, dp);
}
else{
dp[i][j]= max(solve(A,i-1,j,dp),solve(A,i,j-1,dp));
}
return dp[i][j];
}

int Solution::anytwo(string A) {

int  n = A.length();
 vector<vector<int>> dp(n+1,vector<int> (n+1,-1));
int i=n;
int j=n;
return solve(A,i,j,dp)>=2;

}