Tap here to switch tabs
Problem
Submissions

Problem: Minimize Hamming Distance After Swap Operations

med
30 min
Try to solve the Minimize Hamming Distance After Swap Operations problem.

Statement

You are given two integer arrays source and target, both of length n. You are also given an array allowedSwaps where each element allowedSwaps[i] = [ai,bia_i, b_i] indicates that you can swap the elements at index aia_i and index bib_i in the array source.

You may apply any number of swap operations on source using the index pairs from allowedSwaps in any sequence.

The Hamming distance between two arrays of equal length is defined as the total number of indices where their corresponding elements are not equal.

Return the minimum achievable Hamming distance between source and target after applying any number of swap operations on source.

Constraints:

  • n ==== source.length ==== target.length

  • 1≤1 \leq n ≤105\leq 10^5

  • 1≤1 \leq source[i], target[i] ≤105\leq 10^5

  • 0≤0 \leq allowedSwaps.length ≤105\leq 10^5

  • allowedSwaps[i].length ==2== 2

  • 0≤ai,bi≤n−10 \leq a_i, b_i \leq n - 1

  • ai≠bia_i \neq b_i

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimize Hamming Distance After Swap Operations

med
30 min
Try to solve the Minimize Hamming Distance After Swap Operations problem.

Statement

You are given two integer arrays source and target, both of length n. You are also given an array allowedSwaps where each element allowedSwaps[i] = [ai,bia_i, b_i] indicates that you can swap the elements at index aia_i and index bib_i in the array source.

You may apply any number of swap operations on source using the index pairs from allowedSwaps in any sequence.

The Hamming distance between two arrays of equal length is defined as the total number of indices where their corresponding elements are not equal.

Return the minimum achievable Hamming distance between source and target after applying any number of swap operations on source.

Constraints:

  • n ==== source.length ==== target.length

  • 1≤1 \leq n ≤105\leq 10^5

  • 1≤1 \leq source[i], target[i] ≤105\leq 10^5

  • 0≤0 \leq allowedSwaps.length ≤105\leq 10^5

  • allowedSwaps[i].length ==2== 2

  • 0≤ai,bi≤n−10 \leq a_i, b_i \leq n - 1

  • ai≠bia_i \neq b_i