<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>

class Solution:
    def countPairs(self, nums: List[int]) -> int:
        import collections
        dic = collections.Counter(nums)
        neighbor = {}
        ans = 0
        for num in dic:
            s = str(num).zfill(7)
            freq = dic[num]
            if s not in neighbor:
                neighbor[s] = []
            neighbor[s].append([s, freq])
            for i in range(7):
                for j in range(i+1, 7):
                    if s[i] == s[j]:
                        continue
                    new_s = s[:i]+s[j]+s[i+1:j]+s[i]+s[j+1:]
                    if new_s not in neighbor:
                        neighbor[new_s] = []
                    neighbor[new_s].append([s, freq])
        visited = set()
        for s in neighbor:
            children = neighbor[s]
            for i in range(len(children)):
                num_i, freq_i = children[i]
                if (num_i, num_i) not in visited:
                    ans += freq_i * (freq_i - 1)//2
                    visited.add((num_i, num_i))
                for j in range(i+1, len(children)):
                    num_j, freq_j = children[j]
                    if (num_i, num_j) not in visited and (num_j, num_i) not in visited:
                        ans += freq_i * freq_j
                        visited.add((num_i, num_j))
        return ans

        # ans = 0
        # import collections
        # dic = collections.Counter(nums)
        # new_nums = [(k, dic[k]) for k in dic]
        # for i in range(len(new_nums)):
        #     k, v = new_nums[i]
        #     ans += v*(v-1)//2       
        # for i in range(len(new_nums)):
        #     for j in range(i+1, len(new_nums)):
        #         num1, num2 = str(new_nums[i][0]), str(new_nums[j][0])
        #         max_len = max(len(num1), len(nums))
        #         num1 = num1.zfill(max_len)
        #         num2 = num2.zfill(max_len)
        #         diff = [k for k in range(max_len) if num1[k] != num2[k]]
        #         if len(diff) > 4:
        #             continue
        #         nL1 = [num1[k] for k in diff]
        #         nL2 = [num2[k] for k in diff]
        #         if len(diff) <= 3:
        #             nL1 = ''.join(sorted(nL1))
        #             nL2 = ''.join(sorted(nL2))
        #             if nL1 == nL2:
        #                 # print("here2: ", nums[i], nums[j], i, j)
        #                 ans += new_nums[i][1] * new_nums[j][1]
        #         else:
        #             nL11 = ''.join(sorted(nL1))
        #             nL22 = ''.join(sorted(nL2))
        #             if nL11 != nL22:
        #                 continue
        #             flag = False
        #             for k1 in range(3):
        #                 if flag:
        #                     break
        #                 for k2 in range(k1+1, 4):
        #                     if flag:
        #                         break
        #                     k3 = None
        #                     for k3 in range(4):
        #                         if k3 != k1 and k3 != k2:
        #                             break
        #                     k4 = 6 - k1 - k2 - k3
        #                     newnLL1 = [x for x in nL1]
        #                     newnLL2 = [x for x in nL2]
        #                     newnLL1[k1], newnLL1[k2] = newnLL1[k2], newnLL1[k1]
        #                     newnLL1[k3], newnLL1[k4] = newnLL1[k4], newnLL1[k3]
        #                     newnLL1 = ''.join(newnLL1)
        #                     newnLL2 = ''.join(newnLL2)
        #                     if newnLL1 == newnLL2:
        #                         ans += new_nums[i][1] * new_nums[j][1]
        #                         flag = True
        #                         # print("here3: ", nums[i], nums[j], i, j)
        #                         break
        # return ans
        
51146