Skip to content

1128. Number of Equivalent Domino Pairs

  • Time:
  • Space:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
 public:
  int numEquivDominoPairs(vector<vector<int>>& dominoes) {
    int ans = 0;
    unordered_map<int, int> count;

    for (vector<int>& domino : dominoes) {
      int key = min(domino[0], domino[1]) * 10 + max(domino[0], domino[1]);
      ans += count[key];
      ++count[key];
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
  public int numEquivDominoPairs(int[][] dominoes) {
    int ans = 0;
    Map<Integer, Integer> count = new HashMap<>();

    for (int[] domino : dominoes) {
      int key = Math.min(domino[0], domino[1]) * 10 + Math.max(domino[0], domino[1]);
      ans += count.getOrDefault(key, 0);
      count.put(key, count.getOrDefault(key, 0) + 1);
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution:
  def numEquivDominoPairs(self, dominoes: List[List[int]]) -> int:
    ans = 0
    count = Counter()

    for domino in dominoes:
      key = min(domino[0], domino[1]) * 10 + max(domino[0], domino[1])
      ans += count[key]
      count[key] += 1

    return ans
Back to top