Python 3 || 23 lines, dicts  || T/S: 75% / 41%
python3 []\nclass Solution:\n    def countPairs(self, nums: List[int]) -> int:\n\n        def flip_ch(i: int, j: int)-> None:\n\n            if num[i] == num[j]
```python3 []\nclass Solution:\n    def countPairs(self, nums: List[int]) -> int:\n\n        def flip_ch(i: int, j: int)-> None:\n\n            if num[i] == num[j]: return\n            num[i], num[j] = num[j], num[i]\n\n            s = \'\'.join(num)\n\n            if  next[s] < idx + 1:\n                next[s] = idx + 1\n                frst[s] += 1\n\n            return\n\n\n        frst, next, res = defaultdict(int), defaultdict(int), 0\n\n        mx = int(log10(max(nums)))+1\n        nums = list(map(lambda x: (str(x).rjust(mx, \'0\')), nums))\n\n        for idx, num in enumerate(nums):\n\n            res+= frst[num]\n            frst[num]+= 1\n            next[num] = idx + 1\n            num = list(num)\n\n            for c1, c2 in combinations(range(mx), 2):\n                \n                flip_ch(c1, c2)\n\n                for c3, c4 in combinations(range(c1, mx), 2):\n\n                    flip_ch(c3, c4)\n                    num[c3], num[c4] = num[c4], num[c3]\n\n                num[c1], num[c2] = num[c2], num[c1]\n        \n        return res   \n```\n[https://leetcode.com/problems/count-almost-equal-pairs-ii/submissions/1368255972/](https://leetcode.com/problems/count-almost-equal-pairs-ii/submissions/1368255972/)\n\nI could be wrong, but I think that time complexity is *O*(*N* * *D* ^4) and space complexity is *O*(*ND*), in which *N* ~ `len(nums)`and *D* ~ maximum number of digits in `nums`.
['Python3']