Problem · Matrix

Count Matching Regions in Two Binary Grids

Learn this problem
MediumOpenAIOA

Problem statement

You are given two rectangular binary grids, grid1 and grid2. Each grid is represented by an array of strings, where '1' is a filled cell and '0' is an empty cell.

A region is a maximal group of filled cells connected horizontally or vertically. Two regions match when they contain exactly the same cell coordinates, one in each grid.

Return the number of matching regions.

Source note: The report shared the binary-grid input format, the worked sample below, and identified connected-region search as the first coding task. This version closely follows that core task; the original function signature and numeric limits were not shown.

Function

countMatchingRegions(grid1: String[], grid2: String[]) → int

Examples

Example 1

grid1 = ["001","011","100"]grid2 = ["001","011","101"]return = 1

The first grid has regions {(0,2), (1,1), (1,2)} and {(2,0)}. The second has regions {(0,2), (1,1), (1,2), (2,2)} and {(2,0)}. Only the single-cell region matches exactly.

More OpenAI problems

drafts saved locally
public int countMatchingRegions(String[] grid1, String[] grid2) {
  // write your code here
}
grid1["001","011","100"]
grid2["001","011","101"]
expected1
checking account