Given a list of dominoes, dominoes[i] = [a, b] is equivalent to dominoes[j] = [c, d] if and only if either (a == c and b == d), or (a == d and b == c) - that is, one domino can be rotated to be equal to another domino.
Return the number of pairs (i, j) for which 0 <= i < j < dominoes.length, and dominoes[i] is equivalent to dominoes[j].
Example 1:
Input: dominoes = [[1,2],[2,1],[3,4],[5,6]] Output: 1
Example 2:
Input: dominoes = [[1,2],[1,2],[1,1],[1,2],[2,2]] Output: 3
Constraints:
1 <= dominoes.length <= 4 * 104dominoes[i].length == 21 <= dominoes[i][j] <= 9When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're given a set of dominoes and need to find how many pairs are equivalent. The brute force approach will simply compare each domino to every other domino to see if they're the same.
Here's how the algorithm would work step-by-step:
def num_equiv_domino_pairs(dominoes):
number_of_pairs = 0
number_of_dominoes = len(dominoes)
# Iterate through each domino in the list
for first_domino_index in range(number_of_dominoes):
for second_domino_index in range(first_domino_index + 1, number_of_dominoes):
# Ensure we don't compare a domino to itself.
first_domino = dominoes[first_domino_index]
second_domino = dominoes[second_domino_index]
# Check if dominoes are equivalent.
if (first_domino[0] == second_domino[0] and first_domino[1] == second_domino[1]) or \
(first_domino[0] == second_domino[1] and first_domino[1] == second_domino[0]):
number_of_pairs += 1
return number_of_pairsThe key is to realize that the order within a domino pair doesn't matter, so we can treat pairs like [1, 2] and [2, 1] as identical. We can count how many of each unique domino pair we have and then use that to quickly calculate the number of equivalent pairs.
Here's how the algorithm would work step-by-step:
def number_of_equivalent_domino_pairs(dominoes): domino_counts = {}
equivalent_pair_count = 0
for domino in dominoes:
# Ensure consistent ordering for equivalent pairs
domino.sort()
domino_tuple = tuple(domino)
# Count frequency of each domino pair
if domino_tuple in domino_counts:
domino_counts[domino_tuple] += 1
else:
domino_counts[domino_tuple] = 1
for count in domino_counts.values():
# Calculate equivalent pairs using n * (n - 1) / 2
equivalent_pair_count += count * (count - 1) // 2
return equivalent_pair_count| Case | How to Handle |
|---|---|
| Null or Empty input array | Return 0 immediately, as there are no pairs possible. |
| Input array with only one domino | Return 0 because at least two dominoes are needed for a pair. |
| Input array with a large number of dominoes, exceeding available memory | The frequency counting approach is used which should scale well within memory limits by tracking occurrences instead of storing all combinations. |
| All dominoes in the input array are identical (e.g., all [1, 2]) | The frequency counting approach correctly counts pairs when all dominoes are the same. |
| Dominos where both numbers are the same: [1, 1], [2, 2], etc. | The solution correctly accounts for such cases when calculating pair frequencies, as (i, j) is equivalent to (j, i) even when i == j. |
| Input contains very large numbers for domino values exceeding practical limits. | The number of equivalent domino pairs counts occurrences, so large domino values should not cause issues unless memory becomes a constraint. |
| Input with only one distinct type of domino pair. | The algorithm will correctly identify all the pairs of that one domino type and compute the count using the combinatorics formula. |
| Integer overflow when calculating the number of pairs (n*(n-1))/2 for a large number of duplicate dominoes. | Use a larger data type (e.g., long) for counting the pairs to prevent overflow during the calculation. |