LeetCode 1128: Number of Equivalent Domino Pairs Solution
Master LeetCode problem 1128 (Number of Equivalent Domino Pairs), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1128. Number of Equivalent Domino Pairs
Problem Explanation
Explanation
To solve this problem, we can iterate through the given list of dominoes and convert each domino to a canonical representation by ensuring that the smaller number is always in the first position. We can then use a hashmap to store the count of each canonical representation. For each canonical representation encountered, we can calculate the number of pairs that can be formed using the count of that representation. Finally, we sum up the total pairs across all representations.
- Time complexity: O(n), where n is the number of dominoes.
- Space complexity: O(n) to store the counts of the canonical representations.
Solution Code
class Solution {
public int numEquivDominoPairs(int[][] dominoes) {
Map<Integer, Integer> countMap = new HashMap<>();
int pairs = 0;
for (int[] domino : dominoes) {
int val = Math.min(domino[0], domino[1]) * 10 + Math.max(domino[0], domino[1]);
pairs += countMap.getOrDefault(val, 0);
countMap.put(val, countMap.getOrDefault(val, 0) + 1);
}
return pairs;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1128 (Number of Equivalent Domino Pairs)?
This page provides optimized solutions for LeetCode problem 1128 (Number of Equivalent Domino Pairs) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1128 (Number of Equivalent Domino Pairs)?
The time complexity for LeetCode 1128 (Number of Equivalent Domino Pairs) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1128 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1128 in Java, C++, or Python.