<p><strong>Attention</strong>: In this version, the number of operations that can be performed, has been increased to <strong>twice</strong>.<!-- notionvc: 278e7cb2-3b05-42fa-8ae9-65f5fd6f7585 --></p>

<p>You are given an array <code>nums</code> consisting of positive integers.</p>

<p>We call two integers <code>x</code> and <code>y</code> <strong>almost equal</strong> if both integers can become equal after performing the following operation <strong>at most <u>twice</u></strong>:</p>

<ul>
	<li>Choose <strong>either</strong> <code>x</code> or <code>y</code> and swap any two digits within the chosen number.</li>
</ul>

<p>Return the number of indices <code>i</code> and <code>j</code> in <code>nums</code> where <code>i &lt; j</code> such that <code>nums[i]</code> and <code>nums[j]</code> are <strong>almost equal</strong>.</p>

<p><strong>Note</strong> that it is allowed for an integer to have leading zeros after performing an operation.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1023,2310,2130,213]</span></p>

<p><strong>Output:</strong> <span class="example-io">4</span></p>

<p><strong>Explanation:</strong></p>

<p>The almost equal pairs of elements are:</p>

<ul>
	<li>1023 and 2310. By swapping the digits 1 and 2, and then the digits 0 and 3 in 1023, you get 2310.</li>
	<li>1023 and 213. By swapping the digits 1 and 0, and then the digits 1 and 2 in 1023, you get 0213, which is 213.</li>
	<li>2310 and 213. By swapping the digits 2 and 0, and then the digits 3 and 2 in 2310, you get 0213, which is 213.</li>
	<li>2310 and 2130. By swapping the digits 3 and 1 in 2310, you get 2130.</li>
</ul>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,10,100]</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<p>The almost equal pairs of elements are:</p>

<ul>
	<li>1 and 10. By swapping the digits 1 and 0 in 10, you get 01 which is 1.</li>
	<li>1 and 100. By swapping the second 0 with the digit 1 in 100, you get 001, which is 1.</li>
	<li>10 and 100. By swapping the first 0 with the digit 1 in 100, you get 010, which is 10.</li>
</ul>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= nums.length &lt;= 5000</code></li>
	<li><code>1 &lt;= nums[i] &lt; 10<sup>7</sup></code></li>
</ul>

def almost_equal_pairs(nums):
    from collections import defaultdict

    # Step 1: Determine the maximum length of numbers when converted to strings
    max_len = max(len(str(num)) for num in nums)

    # Step 2: Pad numbers with leading zeros and create a mapping
    num_to_indices = defaultdict(list)
    nums_padded = []
    for idx, num in enumerate(nums):
        num_str = str(num).zfill(max_len)
        nums_padded.append(num_str)
        num_to_indices[num_str].append(idx)

    total_count = 0
    n = len(nums)
    for i in range(n):
        s = nums_padded[i]
        permutations = set()
        permutations.add(s)
        s_list = list(s)
        L = len(s_list)

        # Generate permutations via one swap
        for i1 in range(L):
            for i2 in range(i1 + 1, L):
                s1 = s_list.copy()
                s1[i1], s1[i2] = s1[i2], s1[i1]
                permutations.add(''.join(s1))

        # Generate permutations via two swaps
        indices = [(i1, i2) for i1 in range(L) for i2 in range(i1 + 1, L)]
        for idx1 in range(len(indices)):
            i1, j1 = indices[idx1]
            s1 = s_list.copy()
            s1[i1], s1[j1] = s1[j1], s1[i1]
            for idx2 in range(idx1, len(indices)):
                i2, j2 = indices[idx2]
                s2 = s1.copy()
                s2[i2], s2[j2] = s2[j2], s2[i2]
                permutations.add(''.join(s2))

        counted_indices = set()
        for p in permutations:
            if p in num_to_indices:
                for j in num_to_indices[p]:
                    if j > i and j not in counted_indices:
                        total_count += 1
                        counted_indices.add(j)

    return total_count

class Solution:
    def countPairs(self, nums: List[int]) -> int:
        return almost_equal_pairs(nums)
18656