Given a list of dominoes
, dominoes[i] = [a, b]
is equivalentto 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
hash table (naive): 把一个domino pair存入set,再作为key存入map,遍历dominoes,计数每个set出现多少次,最后再计算组合数求pair num
time: O(n), space: O(n)
class Solution { public int numEquivDominoPairs(int[][] dominoes) { Map<Set<Integer>, Integer> map = new HashMap<>(); for(int[] domino : dominoes) { Set<Integer> set = new HashSet<>(); set.add(domino[0]); set.add(domino[1]); map.put(set, map.getOrDefault(set, 0) + 1); } int res = 0; for(Map.Entry<Set<Integer>, Integer> entry: map.entrySet()) { int n = entry.getValue(); if(n >= 2) { res += n * (n - 1) / 2; } } return res; } }
optimized: 把一个domino pair处理成一个两位的数字,再作为key存入map
time: O(n), space: O(n)
class Solution { public int numEquivDominoPairs(int[][] dominoes) { Map<Integer, Integer> map = new HashMap<>(); for(int[] domino : dominoes) { int max = Math.max(domino[0], domino[1]); int min = Math.min(domino[0], domino[1]); int n = min * 10 + max; map.put(n, map.getOrDefault(n, 0) + 1); } int res = 0; for(Map.Entry<Integer, Integer> entry: map.entrySet()) { int n = entry.getValue(); if(n >= 2) { res += n * (n - 1) / 2; } } return res; } }