[Python3] Simple Solution | Time: O(N * maxLength^4) | Space: O(N)
Intuition\n Describe your first thoughts on how to solve this problem. \nWe want to keep track of the count of numbers we\'ve already checked. For the current n
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe want to keep track of the count of numbers we\'ve already checked. For the current number we are checking, we want to try every single possible swapping of digits, while ensuring we don\'t try duplicate swappings. Then we increase our final answer by the count of numbers we\'ve already visited equal to our swapped number (our almost equal pair). \n# Approach\n<!-- Describe your approach to solving the problem. -->\nMake every number the same length by padding with leading 0\'s. Keep track of counts using a dictionary. Use a set to keep track of which swappings we\'ve already tried. Iterate over all the nums. Add the count of numbers we\'ve visited that are equal to nums[i], or a single swapping of nums[i], or a double swapping of nums[i]. Then add num[i] to our counts.\n\n# Complexity\n- Time complexity: O(N * maxLength^4)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nWe traverse the list of numbers in O(N), then for each number we perform all the possible swappings in O(maxLength^4)\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nWe simply keep track of the counts of numbers we\'ve already visited in O(N)\n\n# Code\n```python3 []\nclass Solution:\n    def countPairs(self, nums: List[int]) -> int:\n\n        # keep track of count for prior nums seen\n        visited = defaultdict(lambda:0)\n        # length of longest num\n        n = len(str(max(nums)))\n        # convert nums to strings\n        nums = list(map(str,nums))\n        # intialize our answer\n        ans = 0\n\n        # pad nums till all the same length\n        for i in range(len(nums)):\n            if len(nums[i]) < n:\n                nums[i] = \'0\' * (n-len(nums[i])) + nums[i]\n\n        # brute force every swapping possible\n        for i in range(len(nums)):\n            num = nums[i]\n            # keep track of swappings we\'ve already done so we only count once\n            done = set()\n            # add first the num with no swap & add to \'done\' set\n            ans += visited[num]\n            done.add(num)\n            \n            # swap char at indices k & l\n            for k in range(len(num)):\n                for l in range(k+1,len(num)):\n                    num2 = list(num)\n                    num2[k],num2[l]=num2[l],num2[k]\n                    num2 = \'\'.join(num2)\n                    # if this swap was not done yet, then try it\n                    if num2 not in done:\n                        done.add(num2)\n                        ans += visited[num2]\n                    # swap char at indices a & b\n                    for a in range(len(num2)):\n                        for b in range(k+1,len(num2)):\n                            num3 = list(num2)\n                            num3[a],num3[b]=num3[b],num3[a]\n                            num3 = \'\'.join(num3)\n                            # if this swap was not done yet, then try it\n                            if num3 not in done:\n                                done.add(num3)\n                                ans += visited[num3]\n            # increment the num we just visited to our dictionary\n            visited[num] += 1\n        \n        return ans\n\n\n```
['Python3']