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.merge(key, 1, Integer::sum);
    }

    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 = collections.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