1128. Number of Equivalent Domino Pairs LeetCode Solution
In this guide, you will get 1128. Number of Equivalent Domino Pairs LeetCode Solution with the best time and space complexity. The solution to Number of Equivalent Domino Pairs problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Number of Equivalent Domino Pairs solution in C++
- Number of Equivalent Domino Pairs solution in Java
- Number of Equivalent Domino Pairs solution in Python
- Additional Resources
Problem Statement of 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
Example 2:
Input: dominoes = [[1,2],[1,2],[1,1],[1,2],[2,2]]
Output: 3
Constraints:
1 <= dominoes.length <= 4 * 104
dominoes[i].length == 2
1 <= dominoes[i][j] <= 9
Complexity Analysis
- Time Complexity:
- Space Complexity:
1128. Number of Equivalent Domino Pairs LeetCode Solution in C++
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;
}
};
/* code provided by PROGIEZ */
1128. Number of Equivalent Domino Pairs LeetCode Solution in Java
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;
}
}
// code provided by PROGIEZ
1128. Number of Equivalent Domino Pairs LeetCode Solution in Python
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
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.