website/content.en/ChapterFour/1100~1199/1128.Number-of-Equivalent-Domino-Pairs.md
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
Constraints:
1 <= dominoes.length <= 400001 <= dominoes[i][j] <= 9Given a list dominoes consisting of some dominoes. If one domino can be rotated by 0 degrees or 180 degrees to obtain another domino, we consider these two dominoes equivalent. Formally, the premise for dominoes[i] = [a, b] and dominoes[j] = [c, d] to be equivalent is a==c and b==d, or a==d and b==c.
Under the premise of 0 <= i < j < dominoes.length, find the number of domino pairs (i, j) such that dominoes[i] and dominoes[j] are equivalent.
Note:
package leetcode
func numEquivDominoPairs(dominoes [][]int) int {
if dominoes == nil || len(dominoes) == 0 {
return 0
}
result, buckets := 0, [100]int{}
for _, dominoe := range dominoes {
key, rotatedKey := dominoe[0]*10+dominoe[1], dominoe[1]*10+dominoe[0]
if dominoe[0] != dominoe[1] {
if buckets[rotatedKey] > 0 {
result += buckets[rotatedKey]
}
}
if buckets[key] > 0 {
result += buckets[key]
buckets[key]++
} else {
buckets[key]++
}
}
return result
}