Count how many pairs of dominoes are equivalent when order within each domino does not matter.
Given a list of dominoes, each domino is represented by two numbers. Two dominoes are considered equivalent if one can be rotated to match the other, meaning the pair of values is the same regardless of order.
Your task is to count the number of unordered pairs of equivalent dominoes in the list.
Input Format
- An array of dominoes, where each domino is a pair of integers
[a, b]. - Each pair represents the two values on a domino.
Output Format
- Return an integer: the number of pairs
(i, j)withi < jsuch that dominoiis equivalent to dominoj.
Constraints
- Domino values are assumed to be small non-negative integers.
- The number of dominoes can be large enough that an approach may be too slow.
- A pair is counted once, even if more than two identical dominoes exist.
Example 1
Input
dominoes = [[1,2],[2,1],[3,4],[5,6]]
Output
1
Explanation
Only the first two dominoes are equivalent, so there is exactly one pair.
Example 2
Input
dominoes = [[1,1],[1,1],[1,1]]
Output
3
Explanation
All three dominoes are equivalent. The number of unordered pairs is C(3,2) = 3.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.