Check if Grid Satisfies Conditions
Source: leetcode
Difficulty: Easy
Tags: array, matrix

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`.


 


**Example 1:**



**Input:** grid \= \[\[1,0,2],\[1,0,2]]


**Output:** true


**Explanation:**


**![](https://assets.leetcode.com/uploads/2024/04/15/examplechanged.png)**


All the cells in the grid satisfy the conditions.



**Example 2:**



**Input:** grid \= \[\[1,1,1],\[0,0,0]]


**Output:** false


**Explanation:**


**![](https://assets.leetcode.com/uploads/2024/03/27/example21.png)**


All cells in the first row are equal.



**Example 3:**



**Input:** grid \= \[\[1],\[2],\[3]]


**Output:** false


**Explanation:**


![](https://assets.leetcode.com/uploads/2024/03/31/changed.png)


Cells in the first column have different values.



 


**Constraints:**


- `1 <= n, m <= 10`
- `0 <= grid[i][j] <= 9`




C++ solution:
class Solution {
public:
    bool satisfiesConditions(vector<vector<int>>& grid) {
      for(int i=0;i<grid[0].size();i++) 
      {
        for(int j=0;j<grid.size()-1;j++)
        {
            if(grid[j][i]!=grid[j+1][i])
            {
                return 0;
            }
        }
      } 
       for(int i=0;i<grid.size();i++) 
      {
        for(int j=0;j<grid[0].size()-1;j++)
        {
            if(grid[i][j]==grid[i][j+1])
            {
                return 0;
            }
        }
      } 

      return 1;
    }
};

Go solution:
func satisfiesConditions(grid [][]int) bool {	
    // 1st check row - current value should be different from the cell to its right 
	for i := 0; i < len(grid); i++ {
		row := grid[i]
		for j := 0; j < len(row)-1; j++ {		
			if row[j] == row[j+1] {	return false }
		}
	}
    // 2nd check column - current value should be equal to the cell below 
	for i := 0; i < len(grid)-1; i++ {
		row := grid[i]		
		for j := 0; j < len(row); j++ {			
			if row[j] != grid[i+1][j] { return false }
		}
	}
	
	return true
}

Java solution:
public class Solution {
    public boolean satisfiesConditions(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (j < n - 1 && grid[i][j] == grid[i][j + 1]) {
                    return false;
                }
                if (i < m - 1 && grid[i][j] != grid[i + 1][j]) {
                    return false;
                }
            }
        }
        
        return true;
    }
}

JavaScript solution:
/**
 * @param {number[][]} grid
 * @return {boolean}
 */
var satisfiesConditions = function(g) {
    let i = 0;
    for (let j = 1; j < g[0].length; j++) {
        if (
            g[i][j-1] == g[i][j]
        ) return false
    }
    
    let s = new Set(g.map(e => e.join('')))
    if (s.size > 1) return false 
    return true
};

Python solution:
class Solution:
    def satisfiesConditions(self, grid):
        grid_set = tuple({tuple(row) for row in grid})
        if len(grid_set)==1:
            for item in range(len(grid_set[0])-1):
                if grid_set[0][item]==grid_set[0][item+1]:
                    return False
                
            return True
        return False

Ruby solution:
# @param {Integer[][]} grid
# @return {Boolean}
def satisfies_conditions(grid)
    i = 0
    while i < grid.size
        j = 0
        while j < grid[0].size
            if grid[i][j + 1] and grid[i][j] == grid[i][j + 1]
                return false
            end

            if grid[i+1] and grid[i][j] != grid[i+1][j]
                return false
            end

            j += 1
        end
        i += 1
    end
    
    true
end