<p>You are given two integer arrays, <code>source</code> and <code>target</code>, both of length <code>n</code>. You are also given an array <code>allowedSwaps</code> where each <code>allowedSwaps[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you are allowed to swap the elements at index <code>a<sub>i</sub></code> and index <code>b<sub>i</sub></code> <strong>(0-indexed)</strong> of array <code>source</code>. Note that you can swap elements at a specific pair of indices <strong>multiple</strong> times and in <strong>any</strong> order.</p>

<p>The <strong>Hamming distance</strong> of two arrays of the same length, <code>source</code> and <code>target</code>, is the number of positions where the elements are different. Formally, it is the number of indices <code>i</code> for <code>0 &lt;= i &lt;= n-1</code> where <code>source[i] != target[i]</code> <strong>(0-indexed)</strong>.</p>

<p>Return <em>the <strong>minimum Hamming distance</strong> of </em><code>source</code><em> and </em><code>target</code><em> after performing <strong>any</strong> amount of swap operations on array </em><code>source</code><em>.</em></p>

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

<pre>
<strong>Input:</strong> source = [1,2,3,4], target = [2,1,4,5], allowedSwaps = [[0,1],[2,3]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> source can be transformed the following way:
- Swap indices 0 and 1: source = [<u>2</u>,<u>1</u>,3,4]
- Swap indices 2 and 3: source = [2,1,<u>4</u>,<u>3</u>]
The Hamming distance of source and target is 1 as they differ in 1 position: index 3.
</pre>

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

<pre>
<strong>Input:</strong> source = [1,2,3,4], target = [1,3,2,4], allowedSwaps = []
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are no allowed swaps.
The Hamming distance of source and target is 2 as they differ in 2 positions: index 1 and index 2.
</pre>

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

<pre>
<strong>Input:</strong> source = [5,1,2,4,3], target = [1,5,4,2,3], allowedSwaps = [[0,4],[4,2],[1,3],[1,4]]
<strong>Output:</strong> 0
</pre>

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

<ul>
	<li><code>n == source.length == target.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= source[i], target[i] &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= allowedSwaps.length &lt;= 10<sup>5</sup></code></li>
	<li><code>allowedSwaps[i].length == 2</code></li>
	<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt;= n - 1</code></li>
	<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>

from collections import defaultdict

class Solution:

    def find_parent(self, idx):
        if idx == self.parent_arr[idx]:
            return idx
        self.parent_arr[idx] = self.find_parent(self.parent_arr[idx])
        return self.parent_arr[idx]

    def union(self, idx1, idx2):
        par1, par2 = self.find_parent(idx1), self.find_parent(idx2)
        if par1 == par2:
            return False
        if self.rank[par1] > self.rank[par2]:
            self.parent_arr[par2] = par1
        elif self.rank[par1] < self.rank[par2]:
            self.parent_arr[par1] = par2
        else:
            self.parent_arr[par2] = par1
            self.rank[par1] += 1
        return True

    def minimumHammingDistance(self, source: List[int], target: List[int], allowedSwaps: List[List[int]]) -> int:
        length = len(source)
        self.parent_arr = [idx for idx in range(length)]
        self.rank = [0] * length
        # here allowedSwaps are indices and we will make component by combining each of these swap indices
        for pair in allowedSwaps:
            first_idx, second_idx = pair[0], pair[1]
            self.union(first_idx, second_idx)
        # now we will store each of group in a list of set against parent, parent will have a component associated with it
        idx_groups = defaultdict(set)
        for idx in range(length):
            parent = self.find_parent(idx)
            idx_groups[parent].add(idx)

        hamming_dist = 0
        for group in idx_groups.values():
            # now we will travese each group/component and store each of items in that group along with their frequency,
            # hamming dist for this group will be difference in frequency of each element
            src_items_count = {}
            tar_items_count = {}
            for idx in group:
                src_items_count[source[idx]] = src_items_count.get(source[idx], 0) + 1
                tar_items_count[target[idx]] = tar_items_count.get(target[idx], 0) + 1
            count_diff = 0
            for number, count in src_items_count.items():
                # here we will only consider 1 map count e.g. map1[2: 8, 5: 17], map2[2: 10, 7: 15], so 
                # total unmatched elements will be max(0,(8-10)) + max(0,(17-0)) = 0 + 17 = 17, so this condition handles this
                # max(0, count - tar_items_count.get(number, 0)),  if a element freq diff comes < 0 then another element freq 
                # diff will handle this -ve value like in this case 8's -ve diff is handled by 5's +ve freq diff 17
                count_diff += max(0, count - tar_items_count.get(number, 0))
            hamming_dist += count_diff

        return hamming_dist

64500