💥💥 Beats 100% on runtime and memory [EXPLAINED]
IntuitionIdentify pairs of numbers in the array that can be made identical by swapping digits up to twice. Since the operation allows swapping digits in each nu
# Intuition
Identify pairs of numbers in the array that can be made identical by swapping digits up to twice. Since the operation allows swapping digits in each number, I’ll explore all possible permutations of digits in each number. If two numbers can be made the same after swaps, we count them as a valid pair.

# Approach
I’ll convert each number into a string with a fixed length (by padding zeros) and generate all unique possible digit permutations by swapping digits. Then, I’ll store these permutations in a map to track how many times they appear, and for each number, check how many of its permutations have already appeared to count valid pairs.

# Complexity
- Time complexity:
O(n * maxLen^2), where n is the number of elements in the array and maxLen is the maximum length of the numbers in the array. This is because for each number, I generate all possible permutations by swapping pairs of digits, which involves nested loops.

- Space complexity:
O(n * maxLen^2) as well, because I store all possible permutations for each number in a set and count their occurrences in a map.

# Code
```kotlin []
class Solution {
    // Store the maximum number of digits in the numbers
    private var maxSize = 0

    // Convert a number to a string with leading zeros to match maxSize
    private fun makeDigit(num: Int): String {
        var s = num.toString()
        var n = s.length
        while (n < maxSize) { // Pad with leading zeros until maxSize
            s = "0$s"
            n++
        }
        return s
    }

    // Generate all unique permutations of a number by swapping digits
    private fun makeAllSwaps(num: Int): Set<String> {
        var s = makeDigit(num)
        val res = mutableSetOf(s)

        // Perform swaps between each pair of digits
        for (i in 0 until maxSize) {
            for (j in i + 1 until maxSize) {
                s = s.toCharArray().also { swap(it, i, j) }.joinToString("") // Swap two digits
                res.add(s) // Add the swapped version

                // Swap other digits in the new permutation
                for (l in 0 until maxSize) {
                    for (k in l + 1 until maxSize) {
                        if (s[l] != s[k]) {
                            s = s.toCharArray().also { swap(it, l, k) }.joinToString("") // Swap two more digits
                            res.add(s) // Add this new swapped version
                            s = s.toCharArray().also { swap(it, l, k) }.joinToString("") // Revert the swap
                        }
                    }
                }
                s = s.toCharArray().also { swap(it, i, j) }.joinToString("") // Revert the initial swap
            }
        }

        return res
    }

    // Swap two characters in a char array
    private fun swap(arr: CharArray, i: Int, j: Int) {
        val temp = arr[i]
        arr[i] = arr[j]
        arr[j] = temp
    }

    fun countPairs(nums: IntArray): Int {
        val n = nums.size
        maxSize = nums.maxOrNull()?.toString()?.length ?: 0
        var ans = 0
        val mp = mutableMapOf<String, Int>()

        // Check all numbers and count how many permutations match previous ones
        for (i in 0 until n) {
            for (s in makeAllSwaps(nums[i])) {
                // If this permutation has been encountered, increase the result count
                ans += mp.getOrDefault(s, 0)
            }
            // Add the zero-padded version of the current number to the map
            mp[makeDigit(nums[i])] = mp.getOrDefault(makeDigit(nums[i]), 0) + 1
        }

        return ans
    }
}

```
['Array', 'Hash Table', 'Math', 'String Matching', 'Combinatorics', 'Hash Function', 'Kotlin']