AlgoLounge

Number of Equivalent Domino Pairs

easy

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].

Examples

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 * 10⁴
  • dominoes[i].length == 2
  • 1 <= dominoes[i][j] <= 9
TypeScript
Loading editor…
List of test cases (10)
Case 1
dominoes = [[1,2],[2,1],[3,4],[5,6]]
Case 2
dominoes = [[1,2],[1,2],[1,1],[1,2],[2,2]]
Case 3
dominoes = [[1,2]]
Case 4
dominoes = [[1,1],[1,1],[1,1]]
Case 5
dominoes = [[1,2],[2,1]]
Case 6
dominoes = [[3,5],[5,3],[3,5],[5,3]]
Case 7
dominoes = [[9,9],[9,9],[9,9],[9,9]]
Case 8
dominoes = [[1,2],[3,4],[5,6],[7,8]]
Case 9
dominoes = [[1,1],[2,4],[3,7],[4,1],[5,4],[6,7],[7,1],[8,4],[9,7],[1,1],[2,4],[3,7],[4,1],[5,4],[6,7],[7,1],[8,4],[9,7],[1,1],[2,4]]
Case 10
dominoes = [[1,1],[1,2],[1,3],[1,4],[2,1],[2,2],[2,3],[2,4],[3,1],[3,2],[3,3],[3,4],[4,1],[4,2],[4,3],[4,4]]