Valid Sudoku Medium / #36 / three 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 5 empty empty empty 3 empty empty empty 7 empty 6 empty 1 empty empty empty empty empty empty empty 9 empty empty empty empty 8 empty 4 empty empty empty empty 2 empty empty empty empty empty empty empty 8 empty empty empty empty empty 3 empty empty empty empty 6 empty empty empty empty empty 5 empty empty 7 (repeats) empty empty empty 2 empty empty empty 9 empty empty empty empty empty empty empty empty empty empty 7 (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. walkthrough solve timer --:-- off
switch language → JavaScript TypeScript Python Go Java submit only: no test in the browserC++ submit only: no test in the browserother /**
* @param {character[][]} board
* @return {boolean}
*/
function isValidSudoku(board) {
}
test submit
13 tests · limit 3 s
01 — board = [["1", "2", "3", "4", "5", "6", "7", "8", "9"], ["4", "5", "6", "7", "8", "9", "1", "2", "3"], ["7", "8", "9", "1", "2", "3", "4", "5", "6"], ["2", "3", "4", "5", "6", "7", "8", "9", "1"], ["5", "6", "7", "8", "9", "1", "2", "3", "4"], ["8", "9", "1", "2", "3", "4", "5", "6", "7"], ["3", "4", "5", "6", "7", "8", "9", "1", "2"], ["6", "7", "8", "9", "1", "2", "3", "4", "5"], ["9", "1", "2", "3", "4", "5", "6", "7", "8"]] copy 02 — board = [[".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."]] copy 03 — board = [["5", ".", ".", ".", ".", ".", ".", ".", "5"], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."]] copy 04 — board = [[".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", "2", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", "2", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."]] copy 05 — board = [[".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", "9", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", "9", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."]] copy 06 — board = [[".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", "9", ".", ".", ".", ".", ".", "."], [".", ".", ".", "9", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."]] copy 07 — board = [["6", "9", "8", "7", "3", "4", "1", "2", "5"], ["4", "7", "3", "1", "5", "2", "9", "6", "8"], ["2", "1", "5", "9", "8", "6", "7", "4", "3"], ["9", "8", "2", "3", "6", "7", "5", "1", "4"], ["1", "5", "4", "8", "2", "9", "3", "7", "6"], ["7", "3", "6", "5", "4", "1", "8", "9", "2"], ["3", "6", "9", "4", "7", "5", "2", "8", "1"], ["8", "2", "1", "6", "9", "3", "4", "5", "7"], ["5", "4", "7", "2", "1", "8", "6", "3", "9"]] copy 08 — board = [["1", ".", "2", ".", "4", ".", ".", "5", "."], [".", ".", "5", ".", "1", ".", "8", ".", "."], ["4", "8", "6", ".", ".", ".", ".", ".", "1"], [".", "4", ".", ".", ".", ".", "1", "7", "2"], ["5", ".", ".", ".", "2", ".", "4", ".", "."], ["2", "1", "7", ".", ".", ".", "3", "8", "."], [".", ".", ".", ".", ".", "4", "2", ".", "7"], [".", "5", ".", "2", ".", ".", ".", ".", "9"], ["7", ".", "3", "6", ".", "1", "5", ".", "8"]] copy 09 — board = [[".", ".", "9", ".", ".", ".", ".", "4", "5"], [".", ".", ".", ".", ".", "3", ".", ".", "8"], [".", "6", "2", ".", ".", "4", ".", "3", "."], [".", ".", ".", ".", ".", "5", ".", ".", "."], ["9", ".", "4", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", "2", ".", ".", "8", ".", "5", ".", "3"], [".", ".", ".", ".", ".", ".", ".", "1", "."]] copy 10 — board = [[".", ".", "4", ".", ".", ".", ".", ".", "1"], [".", ".", ".", "8", ".", ".", "8", "3", "."], [".", "8", ".", ".", "6", ".", ".", ".", "."], ["2", ".", ".", ".", ".", ".", "5", ".", "."], ["1", ".", "8", "4", ".", "6", ".", ".", "."], ["4", "5", ".", ".", ".", ".", "3", "1", "."], ["7", "1", "9", ".", ".", ".", "2", "6", "."], [".", "4", ".", ".", ".", ".", ".", ".", "."], [".", "2", "5", "7", ".", ".", ".", ".", "."]] copy 11 — board = [["2", ".", ".", ".", ".", ".", "4", "5", "."], ["7", ".", ".", ".", ".", ".", ".", "3", "1"], ["1", ".", ".", "7", "4", ".", "8", ".", "."], ["3", ".", ".", ".", ".", ".", ".", ".", "6"], ["5", ".", "4", "6", ".", "8", ".", "9", "3"], [".", ".", "8", "3", ".", ".", "2", ".", "."], [".", ".", ".", ".", ".", "2", "3", "5", "."], ["4", ".", ".", ".", ".", ".", ".", ".", "."], ["8", ".", "1", "9", ".", ".", ".", ".", "."]] copy 12 — board = [[".", ".", ".", "6", ".", "1", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "4"], ["4", "6", "1", ".", "9", ".", "8", ".", "."], [".", ".", "5", "4", ".", ".", "9", ".", "."], [".", ".", "7", "5", ".", "8", "4", "6", "."], ["5", ".", ".", ".", ".", ".", ".", "8", "."], [".", ".", "4", ".", ".", ".", ".", "5", "6"], [".", ".", ".", "1", ".", ".", ".", ".", "7"], ["6", ".", ".", "3", ".", "4", ".", ".", "."]] copy 13 — board = [[".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", "1", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", "1", ".", ".", ".", ".", ".", "."]] copy
Stuck? See the solution run step by step on our example, then why it works and its complexity.
Watch the walkthrough → The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.