# Two similar pieces of codes generate different results

I have tried to solve Leetcode #647 and by translating an algorithm using Java to Python. However, I cannot get the same results on the test case "dnncbwoneinoplypwgbwktmvkoimcooyiwirgbxlcttgteqthcvyoueyftiwgwwxvxvg". It looks so weird. Could anyone please figure out what is happening?

``````class Solution(object):
def countSubstrings(self, s):
"""
:type s: str
:rtype: int
"""
count = 0
n = len(s)
visited = [[False]*n]*n
for i in range(n-1, -1, -1):
for j in range(i, n):
visited[i][j] = (s[i] == s[j]) and (j - i < 3 or visited[i+1][j-1])
#if i == 60 and j == 61:
#print "visit 1 time"
#if visited[i][j]:
#print "True"
if visited[i][j]:
count += 1
return count
``````

And the original version proposed by johnyrufus16 is like following:

``````public int countSubstrings(String s) {
int n = s.length();
int res = 0;
boolean[][] dp = new boolean[n][n];
for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
dp[i][j] = s.charAt(i) == s.charAt(j) && (j - i < 3 || dp[i + 1][j - 1]);
if(dp[i][j]) ++res;
}
}
return res;
``````

}

For the above test case, my output is 78 while the answer should be 77. I have tried to print out the result while visited[i][j] == True. It shows that visited[59][62]is true while actually it should be False. And I tried to track it. visited[60][61] is corresponding to the string "gw" and should be False. It is only visited once and should always be False and should be visited before visited[59][62]. However, when i equals to 59 and j equals to 62, visited[60][61] became True! And I added two print in that block. The output showed that also visited[i][j] was only visited once. However, its value are jumping between False and True and finally when the for loop terminated, it restore to False. This is so weird. Could anyone explain it?