Class Solution
java.lang.Object
g1701_1800.s1722_minimize_hamming_distance_after_swap_operations.Solution
1722 - Minimize Hamming Distance After Swap Operations.<p>Medium</p>
<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 <= i <= 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><strong>Example 1:</strong></p>
<p><strong>Input:</strong> source = [1,2,3,4], target = [2,1,4,5], allowedSwaps = [[0,1],[2,3]]</p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong> source can be transformed the following way:</p>
<ul>
<li>
<p>Swap indices 0 and 1: source = [2,1,3,4]</p>
</li>
<li>
<p>Swap indices 2 and 3: source = [2,1,4,3]</p>
</li>
</ul>
<p>The Hamming distance of source and target is 1 as they differ in 1 position: index 3.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> source = [1,2,3,4], target = [1,3,2,4], allowedSwaps = []</p>
<p><strong>Output:</strong> 2</p>
<p><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.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> source = [5,1,2,4,3], target = [1,5,4,2,3], allowedSwaps = [[0,4],[4,2],[1,3],[1,4]]</p>
<p><strong>Output:</strong> 0</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == source.length == target.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= source[i], target[i] <= 10<sup>5</sup></code></li>
<li><code>0 <= allowedSwaps.length <= 10<sup>5</sup></code></li>
<li><code>allowedSwaps[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
minimumHammingDistance
(int[] source, int[] target, int[][] allowedSwaps)
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
minimumHammingDistance
public int minimumHammingDistance(int[] source, int[] target, int[][] allowedSwaps)
-