Longest Same-Color Line
Asked at Google
Reported by the problem's creator, not verified
You are given a rectangular grid of colors. Each cell holds one letter naming its color. A line is a run of cells that are next to each other along a row, along a column, or along either diagonal, and that all have the same color.
from typing import List
def longest_same_color_line(grid: List[str]) -> int:
raise NotImplementedErrorgrid[i] is the i-th row, and grid[i][j] is the color of that cell. Return the number of cells in the longest line.
The grid has at least one cell, so the answer is at least 1. A line along a diagonal goes in a straight diagonal direction, down and right or down and left, with no gaps and no turns.
Examples
Example 1
Input: grid =
["RRRG","RRBB","GRRR","BGBR"]Output:
4Explanation: The cells (0,0), (1,1), (2,2) and (3,3) are all
R, a diagonal line of 4. Row 2 only has a run of 3.