Easy slight variation in the code of LCS


#1

int Solution::anytwo(string A) {
int n=A.length();
if(n==0)
return 0;
string B=A;
int dp[n+1][n+1];
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(A[i-1]==B[j-1] && i!=j)
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
return dp[n][n]>=2;
}