CodeSpeek

Valid Sudoku

Medium · Arrays & Hashing

You are given a 9x9 partially filled Sudoku board represented as a list of 9 rows, each containing 9 characters that are either a digit '1' through '9' or a '.' representing an empty cell. Determine whether the board, in its current partially-filled state, satisfies the standard Sudoku validity rules: each row must not contain the same digit more than once, each column must not contain the same digit more than once, and each of the nine 3x3 sub-boxes (formed by dividing the board into a 3x3 grid of 3x3 blocks) must not contain the same digit more than once. Empty cells ('.') are ignored when checking for duplicates. You only need to validate the filled cells as they currently stand; you do not need to determine if the board can be solved to completion.

Examples

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
Why:    Each row, column and 3x3 sub-box contains no repeated digits, so the board is valid.
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
Why:    The top-left 3x3 sub-box and the first column both contain two 8's, so the board is invalid.

Constraints

board.length == 9, board[i].length == 9, board[i][j] is a digit '1'-'9' or the character '.'

Practise it by voice

Describe the solution out loud and the interviewer writes exactly what you say, asks when you are vague, and runs the tests in your browser.

Practise Valid Sudoku

This statement is written for CodeSpeek. The problem is part of the NeetCode 150 list; Watch NeetCode's explanation of Valid Sudoku. Reference solutions from the NeetCode repository (MIT) are used to verify our tests.