Check if Grid Satisfies Conditions - Problem
You are given a 2D matrix grid of size m x n. You need to check if each cell grid[i][j] is:
- Equal to the cell below it, i.e.
grid[i][j] == grid[i + 1][j](if it exists). - Different from the cell to its right, i.e.
grid[i][j] != grid[i][j + 1](if it exists).
Return true if all the cells satisfy these conditions, otherwise, return false.
Input & Output
Example 1 — Valid Grid
$
Input:
grid = [[1,0,2],[1,0,2]]
›
Output:
true
💡 Note:
All cells in each column are equal (1,1), (0,0), (2,2). Adjacent columns differ: 1≠0, 0≠2. All conditions satisfied.
Example 2 — Invalid Vertical
$
Input:
grid = [[1,1,1],[0,0,0]]
›
Output:
false
💡 Note:
Column values are not uniform: grid[0][0]=1 but grid[1][0]=0. Also adjacent columns are same: grid[0][0]==grid[0][1]. Fails both conditions.
Example 3 — Invalid Horizontal
$
Input:
grid = [[1],[2]]
›
Output:
true
💡 Note:
Single column grid: grid[0][0]=1 equals grid[1][0]=2 is false, but there's no cell below grid[1][0], so vertical condition vacuously true. No right neighbors, so horizontal condition vacuously true.
Constraints
- 1 ≤ m, n ≤ 10
- 1 ≤ grid[i][j] ≤ 9
Visualization
Tap to expand
Understanding the Visualization
1
Input Grid
2D matrix with specific structure requirements
2
Check Conditions
Vertical equality and horizontal difference
3
Output
Boolean result of validation
Key Takeaway
🎯 Key Insight: Grid is valid if columns are uniform and adjacent columns differ
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code