FastPrepCount Connected Equal Tiles

Count Connected Equal Tiles

Intuit logoIntuit● EasyNEW GRADINTERNOA

Problem statement

A rectangular grid contains tile values from 0 to 9. A player selects the tile at zero-based coordinates (row, col). That tile and every tile with the same value connected to it through up, down, left, or right neighbors disappear.

Return the number of tiles that disappear, including the selected tile. Diagonal contact does not connect tiles. Count only this one selected component; there is no falling, chain reaction, or second selection to simulate. Zero is an ordinary tile value.

Function

countDisappearingTiles(grid: int[][], row: int, col: int) → int

Examples

Example 1

grid = [[4,4,4,4],[5,5,5,4],[2,5,7,5]]row = 0col = 0return = 5

The four tiles in the top row and the 4 at (1,3) form one side-connected component of five tiles.

Example 2

grid = [[4,4,4,4],[5,5,5,4],[2,5,7,5]]row = 1col = 0return = 4

The four tiles with value 5 at (1,0), (1,1), (1,2), and (2,1) disappear. The 5 at (2,3) is only diagonally adjacent to this component and remains.

Example 3

grid = [[4,4,4,4],[5,5,5,4],[2,5,7,5]]row = 2col = 2return = 1

The selected 7 has no equal-valued side neighbor, so only that one tile disappears.

Constraints

  • For this exercise, assume 1 <= grid.length <= 100 and every row has the same length from 1 to 100.
  • Every tile is an integer from 0 to 9.
  • 0 <= row < grid.length and 0 <= col < grid[0].length.

More Intuit problems

See Intuit hiring insights
Saved
public int countDisappearingTiles(int[][] grid, int row, int col) {
    // Write your code here
}
grid[[4,4,4,4],[5,5,5,4],[2,5,7,5]]
row0
col0
expected5
Checking account…