Python Hard
python3 []\nclass Solution:\n    def countPairs(self, nums: List[int]) -> int:\n        N = len(nums)\n        \n        lookup = defaultdict(int)\n        \n  
```python3 []\nclass Solution:\n    def countPairs(self, nums: List[int]) -> int:\n        N = len(nums)\n        \n        lookup = defaultdict(int)\n        \n       \n        \n        mx = 0\n        \n        total = 0\n        \n        for i in range(N):\n            nums[i] = str(nums[i])\n            mx = max(mx, len(nums[i]))\n            \n        \n        for i in range(N):\n            while len(nums[i]) < mx:\n                nums[i] = "0" + nums[i]\n                \n            lookup[nums[i]] += 1\n    \n                \n                \n        arr = []\n        \n        for i in range(N):\n            if lookup[nums[i]] != 0:\n                \n                A = [0] * 10\n                for x in nums[i]:\n                    A[int(x)] += 1\n                \n                arr.append((tuple(A), nums[i], lookup[nums[i]]))\n                lookup[nums[i]] = 0\n            \n            \n        d = defaultdict(list)\n        \n\n        \n        for u, v, l in arr:\n            d[u].append((v, l))\n            \n        \n        \n            \n        N = len(arr)\n        \n        \n        # print(arr)\n        \n        for i in range(N):\n            if arr[i][2] > 1:\n                total += math.comb(arr[i][2], 2)\n        \n        \n#         print(total)\n        \n#         print(d)\n\n       \n        for x in list(d.keys()):\n            # print(x)\n            # print(x)\n         \n            \n            B = d[x]\n            N = len(B)\n            # print(B)\n        \n            for i in range(N):\n                for j in range(i + 1, N):\n                    # print(arr[i][0], arr[j][0])\n\n\n\n                    pairs = []\n                    for x, y in zip(B[i][0], B[j][0]):\n                        if x != y:\n                            pairs.append([x, y])\n                            \n                    # print(pairs)\n                    # print(arr)\n\n\n                    if not pairs:\n                        total += (B[i][1] * B[j][1])\n                        continue\n                        \n                    if len(pairs) == 1:\n                        total += (B[i][1] * B[j][1])\n                        continue\n                        \n\n                    if len(pairs) == 2:\n                        # print(total)\n                        total += (B[i][1] * B[j][1])\n                        # print(total)\n                        continue\n\n                    if len(pairs) > 4:\n                        continue\n\n\n\n                    if len(pairs) == 3:\n                        total += (B[i][1] * B[j][1])\n                        continue\n\n\n\n                    flag = True\n\n                    for p in pairs:\n                        if p[::-1] not in pairs:\n                            flag = False\n\n\n                    if flag:\n                        total += (B[i][1] * B[j][1])\n                        \n                        \n \n        return total\n\n\n```
['Python3']