Google questions

Longest Same-Color Line

GoogleEasy

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 NotImplementedError

grid[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.

Part 1: Longest same-color line

Given a rectangular grid of colors, where grid[i][j] is a letter naming the color of that cell, return the number of cells in the longest line of cells that all have the same color.

A line is a straight run of adjacent cells along a row, along a column, or along either diagonal (down-right or down-left), with no gaps and no turns. The grid has at least one cell, so the answer is at least 1.

  1. Example 1
    grid
    ["RRRG","RRBB","GRRR","BGBR"]
    Output
    4

    Why: 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.

Constraints

  • 1 <= len(grid) <= 1000
  • 1 <= len(grid[0]) <= 1000
  • len(grid[i]) == len(grid[0]) for every row grid[i]
  • 'a' <= grid[i][j] <= 'z' or 'A' <= grid[i][j] <= 'Z'
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Longest Same-Color Line: Google interview question | LoopCoach