Leetcode 1128. Number of Equivalent Domino Pairs | Show 24 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 dominoes[i] is equivalent to dominoes[j].
Example 1:
Input: dominoes = [[1,2],[2,1],[3,4],[5,6]]
Output: 1
Solution explained:
1. We'll iterate through this entire array of pairs to find all possible equivalent pairs.
2. We can use a HashMap to help us memorize all the ones that we have already gone through to avoid repeated going through the same pairs for every other pair, this can help us optimize the time complexity from the brute force solution O(n^2) (nested for loop) down to O(n)
3. While building this hashmap, we could find all equivelant pairs in the same iteration, after this loop, we could just safely return the count as the result.
Coding interview made simple!
See the complete solutions to Leetcode problems:
If you like this video and would like to support our work, please share your love here:
Problem URL:
Your comments/thoughts/questions/advice will be greatly appreciated!

0 Comments