Brute Force w/ optimizations
IntuitionThe key insight is that two numbers are "almost equal" if we can make them equal by swapping digits within one number up to 2 times. Therefore, for eac
# Intuition
The key insight is that two numbers are "almost equal" if we can make them equal by swapping digits within one number up to 2 times. Therefore, for each number, we need to generate all possible permutations achievable with ≤2 swaps and check if any previous numbers could match them.

# Approach
1. Process numbers sequentially, for each number:
   - Add count of already seen numbers that match current number
   - Generate all permutations achievable with ≤2 swaps:
     * Pad with zeros for uniform processing
     * Try first swap, then for each first swap try all possible second swaps
     * Store unique permutations in a set
   - Add all permutations to frequency map for future matches

2. Use smart counting technique:
   - Only check matches with previous permutations of preceding numbers
   - Track permutation frequencies in a hash map

# Complexity
- Time complexity: $$O(n * l^4)$$ where n is array length and l is max digits (7)
  * For each number (n) we try all possible first swaps (l^2) and for each try all second swaps (l^2)

- Space complexity: $$O(n * l^4)$$ 
  * Store up to l^4 permutations for each of n numbers
  * In practice much less due to duplicate elimination

# Code
```cpp []
class Solution {
public:
    int countPairs(vector<int>& nums) {
        unordered_map<int, int> num2count;
        int res = 0;

        for (int idx = 0; idx < nums.size(); ++idx) {
            /** Proof of correctness for checking only preceding numbers:
             *  1. Consider any valid pair (A,B) where A and B are almost equal
             *  2. When we process B, all permutations of A are already in num2count
             *  3. If B can become A through ≤2 swaps, then A is one of B's permutations
             *  4. Therefore, when processing B, we'll find A in num2count
             *  5. Conversely, when we processed A, we didn't need B's future permutations
             *     because we'll catch this pair when we reach B */
            res += num2count[nums[idx]]; // Add count of existing numbers that match current number

            string decimal = to_string(nums[idx]);
            /** Pad number with leading zeros to handle all cases uniformly
             *  This ensures we don't miss any valid permutations when swapping digits
             *  1. When we swap digits, any zero moved from padding zone to significant digits
             *     creates a number impossible to get from original (e.g. 123 -> 012)
             *  2. Such numbers can never match with real numbers from input because:
             *     - Input contains only positive integers (no leading zeros)
             *     - Any real number from input, when permuted legally (≤2 swaps),
             *       can't produce a leading zero as all its digits are significant
             *  3. Therefore, padding adds impossible permutations but they never match
             *     with valid numbers, so they don't affect the final count */
            int need_extrasize = max<int>(0, kMax10Pow - decimal.length());
            decimal.resize(decimal.length() + need_extrasize);
            for (int i = decimal.length(); i - need_extrasize >= 0; --i)
                decimal[i] = decimal[i - need_extrasize];
            for (int i = 0; i < need_extrasize; ++i)
                decimal[i] = '0';

            unordered_set<int> permutations; // Unique permutations
            permutations.insert(nums[idx]);

            const int l = decimal.length();
            for (int i = 0; i < l - 1; ++i) {
                for (int j = i + 1; j < l; ++j) if (decimal[i] != decimal[j]) {
                    swap(decimal[i], decimal[j]);
                    permutations.insert(stoi(decimal));
                    for (int p = 0; p < l - 1; ++p) {
                        for (int q = p + 1; q < l; ++q) if (decimal[p] != decimal[q]) {
                            swap(decimal[p], decimal[q]);
                            permutations.insert(stoi(decimal));
                            swap(decimal[p], decimal[q]);
                        }
                    }
                    swap(decimal[i], decimal[j]);
                }
            }

            for (int perm : permutations)
                ++num2count[perm];
        }

        return res;
    }

private:
    static constexpr int kMax10Pow = 7;
};

```
['C++']