Difficulty: Easy
Easy question is light. The comments do the justification here.
We first count the number of equivalent dominoes -> for each count of equivalent dominoes, the number of possible pairs is equal to (count * (count - 1)) // 2
.
We calculate this for all the count of equivalent dominoes and return the result.
class Solution(object):
def numEquivDominoPairs(self, dominoes):
"""
:type dominoes: List[List[int]]
:rtype: int
"""
# dominoes can only be from 1 - 9
# we can identify equivalent dominoes by sorting and making a string
# count this number
# for each domino, we can just add the count sum of count - 1?
# which is count(count - 1) / 2
# if we have 3 of one
# we can do
# 1 and 2, 1 and 3, 2 and 3 (3 pairs)
# if we have 4
# 1 and 2, 1 and 3, 1 and 4, 2 and 3, 2 and 4, 3 and 4 (6 pairs)
count_map = {}
count = 0
for domino in dominoes:
key = str(min(domino)) + str(max(domino))
count_map[key] = count_map.get(key, 0) + 1
for value in count_map.values():
count += ((value * (value - 1)) // 2)
return count
Time Complexity:
O(n)
Space Complexity:
O(n)
Time Taken:
6m 50s