1128. Number of Equivalent Domino Pairs

#### QUESTION:

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 <= 40000 1 <= dominoes[i][j] <= 9

#### SOLUTION:

``````class Solution {
public int numEquivDominoPairs(int[][] dominoes) {
HashMap<int[],Integer> map = new HashMap<>();
for(int i = 0;i<dominoes.length;i++){
int[] tmp = dominoes[i];
Set<Map.Entry<int[], Integer>> entries = map.entrySet();
Iterator<Map.Entry<int[], Integer>> iterator = entries.iterator();
boolean found = false;
inner: while (iterator.hasNext()){
Map.Entry<int[], Integer> next = iterator.next();
int[] key = next.getKey();
if((key[0]==tmp[0] && key[1]==tmp[1]) ||
(key[1]==tmp[0] && key[0]==tmp[1]) ){
next.setValue(next.getValue()+1);
found = true;
break inner;
}
}
if(!found) map.put(tmp,1);
}
int result = 0;
Set<Map.Entry<int[], Integer>> entries = map.entrySet();
Iterator<Map.Entry<int[], Integer>> iterator = entries.iterator();
while (iterator.hasNext()){
Map.Entry<int[], Integer> next = iterator.next();
int value = next.getValue();
if(value != 1){
int tmp = IntStream.range(1,value).sum();
result+=tmp;
}
}
return result;
}

}

//ac中最快的解
class Solution {
public int numEquivDominoPairs(int[][] dominoes) {
int record[][] = new int[9][9];
//平铺出所有的格子，计算出位置
for(int[] x :dominoes){
record[x[0]-1][x[1]-1]++;
}

int count = 0;
//计算出所有相等的count
for(int i=0;i<record.length;i++){
int temp = record[i][i];
count+=(temp*(temp-1)/2);
}

//计算出所有可以rotation的count
for(int i=0;i<record.length;i++){
for(int j=0;j<i;j++){
int temp = record[j][i]+record[i][j];
count+=(temp*(temp-1)/2);
}
}
return count;
}
}
``````
>