Number of Equivalent Domino Pairs

Problem Id: 1128 Difficulty: Easy


Intuition

Solution


class Solution:
    def numEquivDominoPairs(self, dominoes: List[List[int]]) -> int:
        groups = {}
        for index, pair in enumerate(dominoes):
            key = tuple(sorted(pair))
            if key not in groups:
                groups[key] = []
            groups[key].append(index)

        ans = 0
        for index, pair in enumerate(dominoes):
            key = tuple(sorted(pair))
            if len(groups[key]) > 1:
                ans += len(groups[key]) - 1
            groups[key].pop(0)
        return ans