FunctionRacerProblemsRace

Same Letter Region

EasyGridsPython, JavaScript, TypeScript, Java, C++

grid is a list of strings of equal length, one string per row, made of lowercase letters. Start on the cell in row r, column c. From any cell you have reached you may step up, down, left or right (never diagonally) onto a cell that holds the same letter as the starting cell, and continue from there. Return how many different cells you can reach this way, counting the starting cell itself.

Example 1

grid ["aab","abb","ccb"]
r 0
c 0
returns 3

From the top-left a you can reach the a to its right and the a below it: 3 cells.

Example 2

grid ["aab","abb","bcb"]
r 2
c 2
returns 4

The b cells at (2, 2), (1, 2), (1, 1) and (0, 2) are linked side to side: 4 cells. The b at (2, 0) touches that region only at a corner, so it does not count.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Grids problems