Problem statement

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

Solution

We need to hash our dominoes: either use sorted pair, or we can use for example sum of cubes. Then use counter.

Complexity

It is O(n) for time and O(6^2) for space.

Code

class Solution:
    def numEquivDominoPairs(self, dominoes):
        T = Counter(x**3 + y**3 for x, y in dominoes)
        return sum(x*(x-1)//2 for x in T.values())