# 1128. Number of Equivalent Domino Pairs

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`

``````class Solution {
public:
int numEquivDominoPairs(vector<vector<int>>& dominoes) {
int res = 0;
unordered_map<int, int> cntMap;
for (auto &d : dominoes) {
++cntMap[min(d[0], d[1]) * 10 + max(d[0], d[1])];
}
for (auto &a : cntMap) {
res += a.second * (a.second - 1) / 2;
}
return res;
}
};
``````

``````class Solution {
public:
int numEquivDominoPairs(vector<vector<int>>& dominoes) {
int res = 0;
unordered_map<int, int> cntMap;
for (auto &d : dominoes) {
res += cntMap[min(d[0], d[1]) * 10 + max(d[0], d[1])]++;
}
return res;
}
};
``````

Github 同步地址:

https://github.com/grandyang/leetcode/issues/1128

https://leetcode.com/problems/number-of-equivalent-domino-pairs/

https://leetcode.com/problems/number-of-equivalent-domino-pairs/discuss/340022/JavaC%2B%2BPython-Easy-and-Concise

https://leetcode.com/problems/number-of-equivalent-domino-pairs/discuss/339969/JavaPython-3-O(n)-code-with-brief-explanation-and-analysis.

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation