Clean Python Solution
Code\npython3 []\nclass Solution:\n    def countPairs(self, nums: List[int]) -> int:\n        pten = [1, 10, 100, 1000, 10000, 100000, 1000000]\n\n        def s
# Code\n```python3 []\nclass Solution:\n    def countPairs(self, nums: List[int]) -> int:\n        pten = [1, 10, 100, 1000, 10000, 100000, 1000000]\n\n        def swap(num, i, j):\n            numi = (num // pten[i]) % 10\n            numj = (num // pten[j]) % 10\n\n            num += numi * (pten[j] - pten[i])\n            num += numj * (pten[i] - pten[j])\n\n            return num\n\n        @cache\n        def almostEquals(num):\n            equals = {num}\n            for i, j in itertools.combinations(range(7), 2):\n                n = swap(num, i, j)\n                equals.add(n)\n                for k, l in itertools.combinations(range(i + 1, 7), 2):\n                    equals.add(swap(n, k, l))\n\n            return equals\n\n        d = defaultdict(int)\n        tot = 0\n        for n in nums:\n            for e in almostEquals(n):\n                tot += d[e]\n\n            d[n] += 1\n\n        return tot\n\n```
['Python3']