[Python3] find all neighbors via hashmap
Find neighbors by one-pair swap. \nNumbers connected with two-pair swaps share neighbors. \n\n\nclass Solution:\n    def countPairs(self, nums: List[int]) -> in
Find neighbors by one-pair swap. \nNumbers connected with two-pair swaps share neighbors. \n\n```\nclass Solution:\n    def countPairs(self, nums: List[int]) -> int:\n        cnt = Counter(nums)\n        freq = defaultdict(list)\n        for x, v in cnt.items(): \n            freq[x].append((x, v))\n            s = list(str(x).zfill(7))\n            for j in range(7): \n                for k in range(j+1, 7): \n                    if s[j] != s[k]: \n                        s[j], s[k] = s[k], s[j]\n                        cand = int("".join(s))\n                        freq[cand].append((x, v))\n                        s[j], s[k] = s[k], s[j]\n        ans = defaultdict(int)\n        for x, v in freq.items(): \n            for (x1, v1) in v: \n                ans[x1, x1] = v1*(v1-1)//2\n            for (x1, v1), (x2, v2) in combinations(v, 2): \n                ans[x1, x2] = v1*v2 \n        return sum(ans.values())\n```
['Python3']