Check if Grid Satisfies Conditions

easy

By - Aman Pareek

Last Updated - 10/09/2024

Problem Statement

You are provided with a 2D matrix called grid, which has dimensions m x n. Each cell in this matrix contains an integer value. Your task is to determine if this grid adheres to two specific conditions:

  1. Vertical Equality Condition: Every cell in the grid should be equal to the cell directly below it (if such a cell exists). Formally, for each cell grid[i][j], it must be that grid[i][j] == grid[i + 1][j], provided i + 1 is within the bounds of the matrix. This means that each cell's value should match the value of the cell immediately below it in the same column, wherever such a cell exists.

  2. Horizontal Difference Condition: Each cell in the grid should be different from the cell directly to its right (if such a cell exists). In other words, for each cell grid[i][j], it must be that grid[i][j] != grid[i][j + 1], provided j + 1 is within the bounds of the matrix. This ensures that each cell's value is distinct from the value of the cell immediately to its right, wherever such a cell exists.

Your goal is to implement a function that checks if these conditions are satisfied for the entire grid. If every cell meets both conditions, the function should return true. If any cell fails to meet either condition, the function should return false.

Example 1

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

Output: true

Example 2

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

Output: false

Example 3

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

Output: false

Constraints

  • 1 <= m, n <= 10: The number of rows and columns in the grid will be between 1 and 10, inclusive.

  • 0 <= grid[i][j] <= 9: Each cell value is an integer between 0 and 9.

Solution 1: Basic Iteration Approach

function checkGridBasic(grid) {
    const m = grid.length;
    const n = grid[0].length;
    
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (i < m - 1 && grid[i][j] !== grid[i + 1][j]) return false;
            if (j < n - 1 && grid[i][j] === grid[i][j + 1]) return false;
        }
    }
    
    return true;
} 

const grid1 = [[1,0,2],[1,0,2]];
checkGridBasic(grid1);  //output: true 

const grid2 = [[1,1,1],[0,0,0]];
checkGridBasic(grid2);  //output: false 

const grid3 = [[1],[2],[3]];
checkGridBasic(grid3);  //output: false 

Solution 2: Using a Helper Function

function checkGridHelper(grid) {
    function isValid(i, j) {
        if (i < grid.length - 1 && grid[i][j] !== grid[i + 1][j]) return false;
        if (j < grid[0].length - 1 && grid[i][j] === grid[i][j + 1]) return false;
        return true;
    }
    
    for (let i = 0; i < grid.length; i++) {
        for (let j = 0; j < grid[0].length; j++) {
            if (!isValid(i, j)) return false;
        }
    }
    
    return true;
} 

const grid1 = [[1,0,2],[1,0,2]];
checkGridHelper(grid1);  //output: true 

const grid2 = [[1,1,1],[0,0,0]];
checkGridHelper(grid2);  //output: false 

const grid3 = [[1],[2],[3]];
checkGridHelper(grid3);  //output: false 

Solution 3: Row and Column Check

function checkGridRowCol(grid) {
    const m = grid.length;
    const n = grid[0].length;
    
    for (let i = 0; i < m - 1; i++) {
        for (let j = 0; j < n; j++) {
            if (grid[i][j] !== grid[i + 1][j]) return false;
        }
    }
    
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n - 1; j++) {
            if (grid[i][j] === grid[i][j + 1]) return false;
        }
    }
    
    return true;
} 

const grid1 = [[1,0,2],[1,0,2]];
checkGridRowCol(grid1);  //output: true 

const grid2 = [[1,1,1],[0,0,0]];
checkGridRowCol(grid2);  //output: false 

const grid3 = [[1],[2],[3]];
checkGridRowCol(grid3);  //output: false 

Solution 4: Using Array.every Method

function checkGridEvery(grid) {
    const m = grid.length;
    const n = grid[0].length;
    
    return grid.every((row, i) =>
        row.every((value, j) =>
            (i === m - 1 || value === grid[i + 1][j]) &&
            (j === n - 1 || value !== grid[i][j + 1])
        )
    );
} 

const grid1 = [[1,0,2],[1,0,2]];
checkGridEvery(grid1);  //output: true 

const grid2 = [[1,1,1],[0,0,0]];
checkGridEvery(grid2);  //output: false 

const grid3 = [[1],[2],[3]];
checkGridEvery(grid3);  //output: false 

Solution 5: Using Array.some Method

function checkGridSome(grid) {
    const m = grid.length;
    const n = grid[0].length;
    
    return !grid.some((row, i) =>
        row.some((value, j) =>
            (i < m - 1 && value !== grid[i + 1][j]) ||
            (j < n - 1 && value === grid[i][j + 1])
        )
    );
} 

const grid1 = [[1,0,2],[1,0,2]];
checkGridSome(grid1);  //output: true 

const grid2 = [[1,1,1],[0,0,0]];
checkGridSome(grid2);  //output: false 

const grid3 = [[1],[2],[3]];
checkGridSome(grid3);  //output: false 

Solution 6: Using Nested Loops and Flags

function checkGridNestedLoops(grid) {
    const m = grid.length;
    const n = grid[0].length;
    let isValid = true;
    
    for (let i = 0; i < m && isValid; i++) {
        for (let j = 0; j < n && isValid; j++) {
            if (i < m - 1 && grid[i][j] !== grid[i + 1][j]) isValid = false;
            if (j < n - 1 && grid[i][j] === grid[i][j + 1]) isValid = false;
        }
    }
    
    return isValid;
} 

const grid1 = [[1,0,2],[1,0,2]];
checkGridNestedLoops(grid1);  //output: true 

const grid2 = [[1,1,1],[0,0,0]];
checkGridNestedLoops(grid2);  //output: false 

const grid3 = [[1],[2],[3]];
checkGridNestedLoops(grid3);  //output: false 

Solution 7: Using While Loops

function checkGridWhile(grid) {
    const m = grid.length;
    const n = grid[0].length;
    let i = 0;
    
    while (i < m) {
        let j = 0;
        while (j < n) {
            if (i < m - 1 && grid[i][j] !== grid[i + 1][j]) return false;
            if (j < n - 1 && grid[i][j] === grid[i][j + 1]) return false;
            j++;
        }
        i++;
    }
    
    return true;
} 

const grid1 = [[1,0,2],[1,0,2]];
checkGridWhile(grid1);  //output: true 

const grid2 = [[1,1,1],[0,0,0]];
checkGridWhile(grid2);  //output: false 

const grid3 = [[1],[2],[3]];
checkGridWhile(grid3);  //output: false 

Solution 8: Using Map for Validity Check

function checkGridMap(grid) {
    const m = grid.length;
    const n = grid[0].length;
    const checkMap = new Map();
    
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (i < m - 1 && grid[i][j] !== grid[i + 1][j]) checkMap.set('row', false);
            if (j < n - 1 && grid[i][j] === grid[i][j + 1]) checkMap.set('col', false);
        }
    }
    
    return !checkMap.has('row') && !checkMap.has('col');
} 

const grid1 = [[1,0,2],[1,0,2]];
checkGridMap(grid1);  //output: true 

const grid2 = [[1,1,1],[0,0,0]];
checkGridMap(grid2);  //output: false 

const grid3 = [[1],[2],[3]];
checkGridMap(grid3);  //output: false 

Solution 9: Using Functional Programming (Filter)

function checkGridFilter(grid) {
    const rowViolations = grid.flatMap((row, i) =>
        row.flatMap((value, j) => (i < grid.length - 1 && value !== grid[i + 1][j]) ? [false] : [])
    );
    
    const colViolations = grid.flatMap((row, i) =>
        row.flatMap((value, j) => (j < row.length - 1 && value === row[j + 1]) ? [false] : [])
    );
    
    return rowViolations.length === 0 && colViolations.length === 0;
} 

const grid1 = [[1,0,2],[1,0,2]];
checkGridFilter(grid1);  //output: true 

const grid2 = [[1,1,1],[0,0,0]];
checkGridFilter(grid2);  //output: false 

const grid3 = [[1],[2],[3]];
checkGridFilter(grid3);  //output: false 

Solution 10: Using Recursive Function

function checkGridRecursive(grid) {
    function check(i, j) {
        if (i >= grid.length) return true;
        if (j >= grid[0].length) return check(i + 1, 0);
        
        if (i < grid.length - 1 && grid[i][j] !== grid[i + 1][j]) return false;
        if (j < grid[0].length - 1 && grid[i][j] === grid[i][j + 1]) return false;
        
        return check(i, j + 1);
    }
    
    return check(0, 0);
} 

const grid1 = [[1,0,2],[1,0,2]];
checkGridRecursive(grid1);  //output: true 

const grid2 = [[1,1,1],[0,0,0]];
checkGridRecursive(grid2);  //output: false 

const grid3 = [[1],[2],[3]];
checkGridRecursive(grid3);  //output: false 

Solution 11: Using Reduce for Validation

function checkGridReduce(grid) {
    const m = grid.length;
    const n = grid[0].length;
    
    return grid.reduce((valid, row, i) => {
        return valid && row.reduce((rowValid, value, j) => {
            if (i < m - 1 && value !== grid[i + 1][j]) return false;
            if (j < n - 1 && value === grid[i][j + 1]) return false;
            return rowValid;
        }, true);
    }, true);
} 

const grid1 = [[1,0,2],[1,0,2]];
checkGridReduce(grid1);  //output: true 

const grid2 = [[1,1,1],[0,0,0]];
checkGridReduce(grid2);  //output: false 

const grid3 = [[1],[2],[3]];
checkGridReduce(grid3);  //output: false 

Solution 12: Using Iteration with Flags

function checkGridFlags(grid) {
    const m = grid.length;
    const n = grid[0].length;
    let isValid = true;
    
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (i < m - 1 && grid[i][j] !== grid[i + 1][j]) {
                isValid = false;
                break;
            }
            if (j < n - 1 && grid[i][j] === grid[i][j + 1]) {
                isValid = false;
                break;
            }
        }
        if (!isValid) break;
    }
    
    return isValid;
} 

const grid1 = [[1,0,2],[1,0,2]];
checkGridFlags(grid1);  //output: true 

const grid2 = [[1,1,1],[0,0,0]];
checkGridFlags(grid2);  //output: false 

const grid3 = [[1],[2],[3]];
checkGridFlags(grid3);  //output: false 

Solution 13: Using Generators

function checkGridGenerators(grid) {
    for (const [i, j] of traverseGrid(grid)) {
        if (i < grid.length - 1 && grid[i][j] !== grid[i + 1][j]) return false;
        if (j < grid[0].length - 1 && grid[i][j] === grid[i][j + 1]) return false;
    }
    
    return true;
}
function* traverseGrid(grid) {
    const m = grid.length;
    const n = grid[0].length;
    
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            yield [i, j];
        }
    }
} 

const grid1 = [[1,0,2],[1,0,2]];
checkGridGenerators(grid1);  //output: true 

const grid2 = [[1,1,1],[0,0,0]];
checkGridGenerators(grid2);  //output: false 

const grid3 = [[1],[2],[3]];
checkGridGenerators(grid3);  //output: false 

Popular Solutions