Match Three Plus in Grid - aloalgo

Match Three Plus in Grid

Hard

You are given a rectangular grid of numbers, represented as grid.

Your task is to identify groups of three or more identical, adjacent numbers in a row or column within the grid and modify them by setting all numbers in such groups to 0.

Note that:

  • Two numbers are considered adjacent if they are directly next to each other horizontally or vertically (not diagonally).

Return the modified grid.

Example 1

Input
[
  [1, 1, 1, 2],
  [1, 2, 2, 3],
  [3, 3, 2, 1]
]
Output
[
  [0, 0, 0, 2],
  [1, 2, 2, 3],
  [3, 3, 2, 1]
]
Explanation:

The top row has three adjacent '1's. This group is zeroed out. The '1' in the 2nd column is not part of the group.

Example 2

Input
[
  [7, 2, 7, 2],
  [7, 7, 7, 7],
  [3, 3, 7, 7]
]
Output
[
  [7, 2, 0, 2],
  [0, 0, 0, 0],
  [3, 3, 0, 7]
]
Explanation:

The 7's in the 3rd column are zeroed out along with the 7's in the 2nd row.

Example 3

Input
[
  [7, 3, 3, 3],
  [7, 5, 7, 7],
  [2, 2, 2, 7]
]
Output
[
  [7, 0, 0, 0],
  [7, 5, 7, 7],
  [0, 0, 0, 7]
]
Explanation:

The 3's in the first row are zeroed out, along with the 2's in the last row.

Loading...
Input
[
  [1, 1, 1, 2],
  [1, 2, 2, 3],
  [3, 3, 2, 1]
]
Output
[
  [0, 0, 0, 2],
  [1, 2, 2, 3],
  [3, 3, 2, 1]
]

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!