Skip to content
BytePatterns

Same Value Along Every Diagonal

EasyMatrix & Grid#matrix-indexing#diagonals~15m

Problem

A test pattern for a display is stored as a grid of numbers. It is valid only when every diagonal running from top-left to bottom-right holds a single repeated value. Given the grid, return True if it is valid and False otherwise. The grid has between 1 and 20 rows and between 1 and 20 columns, and each value is between 0 and 99.

Examples

Input:  grid = [[1, 2, 3, 4], [5, 1, 2, 3], [9, 5, 1, 2]]
Output: True
Why:    the diagonals are [9], [5, 5], [1, 1, 1], [2, 2, 2], [3, 3] and [4]
Input:  grid = [[1, 2], [2, 2]]
Output: False
Why:    the main diagonal holds 1 and then 2
Input:  grid = [[7]]
Output: True
Why:    edge case, a single cell is one diagonal with one value

Hints

0 / 3

Stuck on the idea rather than the code? Rotate In Place covers it.