Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
The source array can be imagined as a graph where each index is a node and each allowedSwaps[i] is an edge.
Nodes within the same component can be freely swapped with each other.
For each component, find the number of common elements. The elements that are not in common will contribute to the total Hamming distance.