Valid Sudoku

Medium#36three sets

You get a 9 × 9 sudoku board, partly filled. Check that it breaks no rule: a digit may not repeat in a row, in a column or in any of the nine 3 × 3 boxes. You do not have to solve it. Our board has 17 digits, and the answer is false: two 7s share the bottom-right box, though their rows and columns differ.

our board
5emptyemptyempty3emptyemptyempty7
empty6empty1emptyemptyemptyemptyempty
emptyempty9emptyemptyemptyempty8empty
4emptyemptyemptyempty2emptyemptyempty
emptyemptyemptyempty8emptyemptyemptyempty
empty3emptyemptyemptyempty6emptyempty
emptyemptyempty5emptyempty7 (repeats)emptyempty
empty2emptyemptyempty9emptyemptyempty
emptyemptyemptyemptyemptyemptyempty7 (repeats)empty
marked: the digits that repeat in a row, a column or a box
input
board: a 9 × 9 Sudoku board, partly filled: each cell is a digit '1'–'9' or '.' for an empty one.
returns
true if no digit repeats in any row, any column or any of the nine 3 × 3 boxes, otherwise false. Only the filled cells count: the board need not be solvable.
constraints
  • board.length = 9, board[i].length = 9
  • board[i][j]: a digit '1'–'9' or '.'
our 13 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {character[][]} board
 * @return {boolean}
 */
function isValidSudoku(board) {

}
Ctrl ↵
format
reset

saved in this browser

13 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more hash map problems

Pattern “Hash map” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.