Formatted question description: https://leetcode.ca/all/1722.html

# 1722. Minimize Hamming Distance After Swap Operations

Medium

## Description

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

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

Return the minimum Hamming distance of source and target after performing any amount of swap operations on array source.

Example 1:

Input: source = [1,2,3,4], target = [2,1,4,5], allowedSwaps = [[0,1],[2,3]]

Output: 1

Explanation: source can be transformed the following way:

• Swap indices 0 and 1: source = [2,1,3,4]
• Swap indices 2 and 3: source = [2,1,4,3]

The Hamming distance of source and target is 1 as they differ in 1 position: index 3.

Example 2:

Input: source = [1,2,3,4], target = [1,3,2,4], allowedSwaps = []

Output: 2

Explanation: 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.

Example 3:

Input: source = [5,1,2,4,3], target = [1,5,4,2,3], allowedSwaps = [[0,4],[4,2],[1,3],[1,4]]

Output: 0

Constraints:

• n == source.length == target.length
• 1 <= n <= 10^5
• 1 <= source[i], target[i] <= 10^5
• 0 <= allowedSwaps.length <= 10^5
• allowedSwaps[i].length == 2
• 0 <= a_i, b_i <= n - 1
• a_i != b_i

## Solution

Use union set to find the disjoint sets of indices, where the indices in each set can be swapped from allowedSwaps. For each disjoint set, obtain the number of occurrences of each number in source and target. If the numbers of occurrences of a number is difference from the two arrays, then calculate the Hamming Distance using the sum of absolute differences divided by 2.

After all disjoint sets’ Hamming Distances are calculated, return the sum of all Hamming Distances.

class Solution {
public int minimumHammingDistance(int[] source, int[] target, int[][] allowedSwaps) {
int length = source.length;
int[] parent = new int[length];
for (int i = 0; i < length; i++)
parent[i] = i;
for (int[] swap : allowedSwaps) {
int index1 = swap[0], index2 = swap[1];
if (find(parent, index1) != find(parent, index2))
union(parent, index1, index2);
}
Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
for (int i = 0; i < length; i++) {
int root = find(parent, i);
List<Integer> indices = map.getOrDefault(root, new ArrayList<Integer>());
map.put(root, indices);
}
int distance = 0;
for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {
Map<Integer, Integer> counts = new HashMap<Integer, Integer>();
List<Integer> indices = entry.getValue();
int size = indices.size();
for (int i = 0; i < size; i++) {
int index = indices.get(i);
int num1 = source[index], num2 = target[index];
int count1 = counts.getOrDefault(num1, 0) + 1;
if (count1 != 0)
counts.put(num1, count1);
else
counts.remove(num1);
int count2 = counts.getOrDefault(num2, 0) - 1;
if (count2 != 0)
counts.put(num2, count2);
else
counts.remove(num2);
}
for (Map.Entry<Integer, Integer> countEntry : counts.entrySet())
distance += Math.abs(countEntry.getValue());
}
return distance / 2;
}

public void union(int[] parent, int index1, int index2) {
parent[find(parent, index1)] = find(parent, index2);
}

public int find(int[] parent, int index) {
while (parent[index] != index) {
parent[index] = find(parent, parent[index]);
index = parent[index];
}
return index;
}
}