Short solution [Python3]
Approach\nProblem is as simple as count equal numbers pairs, just do this with permutations.\n\ngo function generates all possible permutations within 2 steps.\
# Approach\nProblem is as simple as count equal numbers pairs, just do this with permutations.\n\n`go` function generates all possible permutations within 2 steps.\n\n# Code\n```python3 []\nclass Solution:\n    def countPairs(self, nums: List[int]) -> int:\n        @cache\n        def go(num):\n            cur = [list(str(num))]\n            s = set()\n            s.add(\'\'.join(cur[0]))\n            for _ in range(2):\n                nxt = []\n                for n in cur:\n                    for i in range(len(n)):\n                        for j in range(i + 1, len(n)):\n                            nn = n.copy()\n                            nn[i], nn[j] = nn[j], nn[i]\n                            snn = \'\'.join(nn).lstrip(\'0\')\n                            if snn not in s:\n                                s.add(snn)\n                                nxt.append(nn)\n                cur = nxt\n            return s\n        \n        ret = 0\n        cnt = defaultdict(int)\n        for num in sorted(nums, reverse=True):\n            ret += cnt[str(num)]\n            for sub in go(num):\n                cnt[sub] += 1\n        return ret\n        \n```
['Python3']