Ctrl K

Constant Diagonals

Easy
You are given a rectangular m × n integer table grid. A diagonal is the set of cells you visit by starting at any cell and stepping down and to the right until you leave the table. Return true if every such diagonal contains a single repeated value, and false otherwise.
Example 1
grid=[[7, 0, 3], [8, 7, 0], [5, 8, 7], [6, 5, 8]] → True

Every down-right diagonal holds one value: [6], [5, 5], [8, 8, 8], [7, 7, 7], [0, 0], [3].

Example 2
grid=[[4, 1], [2, 5]] → False

The main diagonal holds 4 and 5, which differ.

Example 3
grid=[[9, 9, 9, 9]] → True

With a single row, every diagonal is one cell long, so the condition holds.

Constraints
  • 1 ≤ m, n ≤ 20
  • 0 ≤ grid[i][j] ≤ 99
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCrossword That Reads Both Ways EasyIsland Coastline Length EasyRepack the Tray EasyBlur the Pixels EasyPaint Bucket Tool Easy
loading Python…
Input
grid=[[7, 0, 3], [8, 7, 0], [5, 8, 7], [6, 5, 8]]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 87 tests on the server, hidden ones included.