Number of Equivalent Domino Pairs LeetCode solution in Python

1
import math
2

3

4
class Solution:
5
def numEquivDominoPairs(self, dominoes: List[List[int]]) -> int:
6
d = dict()
7
for i in dominoes:
8
i.sort() # Just to make everything equal and comparable
9
if (
10
tuple(i) in d.keys()
11
): # In python, lists are unhashable so converted the list into tuples
12
d[tuple(i)] += 1
13
else:
14
d[tuple(i)] = 1
15
count = 0
16
for x, y in d.items():
17
if y > 1:
18
count += (
19
y * (y - 1) // 2
20
) # To check the number of pairs, if 2 elements pairs is 1,if 3 pair is 3 and so on.....formula is n*n-1/2
21
return count

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0