Skip to main content
Back to problems
#3142
Easy Algorithms

Check if grid satisfies conditions

Array Matrix
45.0% acceptance
Feb 24, 2026
113
6
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.

Solution

Rust
Time O(n²)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn satisfies_conditions(grid: Vec<Vec<i32>>) -> bool {
    let m = grid.len();
    let n = grid[0].len();
    for r in 0..m {
      for c in 0..n {
        if r + 1 < m && grid[r][c] != grid[r + 1][c] {
          return false;
        }
        if c + 1 < n && grid[r][c] == grid[r][c + 1] {
          return false;
        }
      }
    }
    true
  }
}