Number of Equivalent Domino Pairs (LeetCode 1128): Solution and Explanation

Easy
Array
Hash Table
Counting

Count the index pairs of dominoes that are the same tile, allowing either tile to be turned around.

Intuition

Two dominoes are equivalent when they hold the same two numbers, in either order. Writing every tile with its smaller number first makes equivalent tiles look exactly the same.

Once tiles are normalized, the question becomes: how many pairs of equal items are there? Group the tiles by shape and count.

Counting can happen on the fly. When a tile arrives, it forms a pair with every earlier tile of the same shape, so add the current pile size to the answer and then put the tile on the pile.

Approach: Canonical key and running count

  1. Create count[100], all zero, and pairs = 0.
  2. For each domino [a, b], compute key = min(a, b) * 10 + max(a, b). Values are 1 to 9, so each shape gets its own key below 100.
  3. Add count[key] to pairs: the new tile pairs with each earlier tile of that shape.
  4. Increment count[key]. Return pairs at the end.

Step-by-step example

dominoes = [[1,2],[2,1],[3,4],[5,6]]
  1. [1,2] has key 12. The pile for 12 is empty, so no pair. count[12] = 1.
  2. [2,1] turns around to [1,2], key 12. One tile is already there, so pairs = 1. count[12] = 2.
  3. [3,4] has key 34, a new pile. No pair.
  4. [5,6] has key 56, a new pile. No pair. Return 1.

Code: Python, Java, C++

1class Solution:
2    def numEquivDominoPairs(self, dominoes: List[List[int]]) -> int:
3        count = [0] * 100
4        pairs = 0
5        for a, b in dominoes:
6            key = a * 10 + b if a <= b else b * 10 + a
7            pairs += count[key]
8            count[key] += 1
9        return pairs
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each domino is handled once with constant work.

Space

O(1)

A fixed array of 100 counters, whatever the input size.

Other approaches

Count, then sum c * (c - 1) / 2 (time O(n), space O(1))

Count every shape first, then add c * (c - 1) / 2 for each pile. Same result and complexity, in two passes.

Compare every pair (time O(n^2), space O(1))

Check all i < j directly. Correct but O(n^2), about 8 * 10^8 checks at the limit.

Common mistakes

  • Forgetting to normalize, so [1,2] and [2,1] land in different piles.
  • Incrementing the count before adding it, which counts each tile as a pair with itself.
  • Using a + b or a * b as the key: [1,4] and [2,3] share a sum, and [1,6] and [2,3] share a product.

Frequently asked questions

Why add count[key] before incrementing?

The new tile pairs only with tiles that came before it. Adding first counts those earlier tiles; incrementing first would also pair the tile with itself.

Why is min * 10 + max a safe key?

Both values are single digits from 1 to 9, so the key is a two-digit number whose digits are exactly the two halves. Different shapes can never collide.

Can the answer overflow a 32-bit integer?

No. With 4 * 10^4 identical tiles the answer is about 8 * 10^8, which still fits.