[Python3] Brute Force
Let\'s write a function adj(int) -> set(int) that describes which y are possible to reach from x in at most 2 operations.\n\nNote we only need to care about swa
Let\'s write a function `adj(int) -> set(int)` that describes which `y` are possible to reach from `x` in at most 2 operations.\n\nNote we only need to care about swaps in one direction if we sort the array.\n\n# Code\n```python3 []\nclass Solution:\n    def countPairs(self, A: List[int]) -> int:\n        def adj(x):\n            s = list(str(x))\n            n = len(s)\n            ans = set()\n            for i, j in combinations_with_replacement(range(n), 2):\n                s[i], s[j] = s[j], s[i]\n                for k, l in combinations_with_replacement(range(n), 2):\n                    s[k], s[l] = s[l], s[k]\n                    ans.add(int("".join(s)))\n                    s[k], s[l] = s[l], s[k]\n                s[i], s[j] = s[j], s[i]\n            return ans\n\n        A.sort(reverse=True)\n        ans = 0\n        count = Counter()\n        for x in A:\n            ans += count[x]\n            for y in adj(x):\n                count[y] += 1\n        return ans\n```
['Python3']