Longest Consecutive Same-Color Line
Problem statement
You are given a non-empty rectangular grid of uppercase color symbols. The grid is represented by grid, where grid[row][column] is the color at that cell.
A same-color line is a contiguous sequence of cells that all contain the same symbol. A line may extend in exactly one of these four orientations:
- horizontally from left to right,
- vertically from top to bottom,
- diagonally down and right, or
- diagonally down and left.
Return the maximum number of cells in any same-color line.
Function
longestSameColorLine(grid: String[]) → intExamples
Example 1
grid = ["RRRG","RRBB","GRRR","BGBR"]return = 4The cells from (0, 0) through (3, 3) form a down-right diagonal of four R symbols. No longer line exists.
Example 2
grid = ["RRGGGG"]return = 4The only row ends with four consecutive G symbols, so the longest line has length 4.
Example 3
grid = ["R","G","G","G"]return = 3The single column contains a vertical run of three consecutive G symbols.
Constraints
1 <= grid.length <= 1000.1 <= grid[i].length <= 1000.- Every row has the same length.
- The total number of cells is at most
100000. - Every cell contains one uppercase English letter from
AthroughZ.