25. [matrix] Valid Sudoku

36. Valid Sudoku

Medium

8.9K

933

Companies

Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  1. Each row must contain the digits 1-9 without repetition.

  2. Each column must contain the digits 1-9 without repetition.

  3. Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.

Note:

  • A Sudoku board (partially filled) could be valid but is not necessarily solvable.

  • Only the filled cells need to be validated according to the mentioned rules.

Example 1:

!https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png

Input: board =
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output: true

Example 2:

Input: board =
[["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output: false
Explanation: Same as Example 1, except with the5 in the top left corner being modified to8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.

Constraints:

  • board.length == 9

  • board[i].length == 9

  • board[i][j] is a digit 1-9 or '.'.

Time: O(1)

Space: O(1)

/**
 * @param {character[][]} board
 * @return {boolean}
 */
const points = [
    [0, 0],
    [0, 3],
    [0, 6],
    [3, 0],
    [3, 3],
    [3, 6],
    [6, 0],
    [6, 3],
    [6, 6],
];

const set = new Set();

var isValidSudoku = function(board) {
    let flag = true;

    for (let i = 0; i < 9; i++) {
        set.clear();
        flag = checkRow(i, board);
        if (!flag) return false;
        
        set.clear();
        flag = checkColumn(i, board);
        if (!flag) return false;
    }

    for (const [row, col] of points) {
        set.clear();
        flag = check(row, col, board);
        if (!flag) return false;
    }

    return flag;
};

function checkRow(col, board) {
    for (let i = 0; i < 9; i++) {
        const curr = board[i][col];
        if (curr === '.') 
            continue;
        if (set.has(curr)) 
            return false;
        set.add(curr);
    }
    return true;
}

function checkColumn(row, board) {
    for (let i = 0; i < 9; i++) {
        const curr = board[row][i];
        if (curr === '.') 
            continue;
        if (set.has(curr)) 
            return false;
        set.add(curr);
    }
    return true;
}

function check(row, col, board) {
    for (let i = 0; i < 3; i++) {
        for (let j = 0; j < 3; j++) {
            const curr = board[row + i][col + j];
            if (curr === '.') 
                continue;
            if (set.has(curr))
                return false;

            set.add(curr);
        }
    }
    return true;
}

Last updated