Problem · String

Count Distinct Domino Colorings

Learn this problem
MediumAmazonINTERNOA
See Amazon hiring insights

Problem statement

In Amazon's warehouse automation systems, robotic movement paths are modeled using domino-like tiles placed on a grid.

A domino is a unique 1x2 or 2x1 piece represented by an English character, lowercase or uppercase. A valid domino arrangement is represented by two strings in the array domino.

Treat domino[0] as the top row and domino[1] as the bottom row of a 2 * n grid. Both strings have the same length n.

Each character appears exactly twice and identifies one domino. The two strings always describe a complete, non-overlapping tiling of the 2 x n grid:

  • There is a vertical domino at column i if domino[0][i] == domino[1][i].
  • There is a horizontal domino spanning columns i and i + 1 if domino[0][i] == domino[0][i + 1] and domino[1][i] == domino[1][i + 1].

Two dominoes are adjacent if they share a side in this grid, up, down, left, or right. Diagonal contact does not count.

Each domino can be colored using one of three colors: Red, Green, or Blue, represented as RGB. Both halves of a domino must be the same color, and adjacent dominoes must have different colors.

Find the number of distinct ways to color the dominoes such that no two adjacent dominoes have the same color. Return the answer modulo 10^9 + 7.

Function

countDistinctColorings(domino: String[]) → int

Examples

Example 1

domino = ["abb", "acc"]return = 6

The arrangement can be visualized as:

a b b (top)
a c c (bottom)

The valid colorings are:

  • [RGG, RBB]
  • [RBB, RGG]
  • [GRR, GBB]
  • [GBB, GRR]
  • [BGG, BRR]
  • [BRR, BGG]

The first domino, a, is arranged vertically. The b and c dominoes are horizontal. The answer is 6.

More Amazon problems

drafts saved locally
public int countDistinctColorings(String[] domino) {
  // write your code here
}
domino["abb", "acc"]
expected6
checking account