for j in range(m+1): for k in range(n+1): if j==0and k==0: continue if j==0and dp[j][k-1]==1and s3[j+k-1]==s1[k-1]: dp[j][k]=1 elif k==0and dp[j-1][k]==1and s3[j+k-1]==s2[j-1]: dp[j][k]=1 elif dp[j-1][k]==1and s3[j+k-1]==s2[j-1] or dp[j][k-1]==1and s3[j+k-1]==s1[k-1]: dp[j][k]=1