|
| 1 | +<p>Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated <strong>according to the following rules</strong>:</p> |
| 2 | + |
| 3 | +<ol> |
| 4 | + <li>Each row must contain the digits <code>1-9</code> without repetition.</li> |
| 5 | + <li>Each column must contain the digits <code>1-9</code> without repetition.</li> |
| 6 | + <li>Each of the 9 <code>3x3</code> sub-boxes of the grid must contain the digits <code>1-9</code> without repetition.</li> |
| 7 | +</ol> |
| 8 | + |
| 9 | +<p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png" style="height:250px; width:250px" /><br /> |
| 10 | +<small>A partially filled sudoku which is valid.</small></p> |
| 11 | + |
| 12 | +<p>The Sudoku board could be partially filled, where empty cells are filled with the character <code>'.'</code>.</p> |
| 13 | + |
| 14 | +<p><strong>Example 1:</strong></p> |
| 15 | + |
| 16 | +<pre> |
| 17 | +<strong>Input:</strong> |
| 18 | +[ |
| 19 | + ["5","3",".",".","7",".",".",".","."], |
| 20 | + ["6",".",".","1","9","5",".",".","."], |
| 21 | + [".","9","8",".",".",".",".","6","."], |
| 22 | + ["8",".",".",".","6",".",".",".","3"], |
| 23 | + ["4",".",".","8",".","3",".",".","1"], |
| 24 | + ["7",".",".",".","2",".",".",".","6"], |
| 25 | + [".","6",".",".",".",".","2","8","."], |
| 26 | + [".",".",".","4","1","9",".",".","5"], |
| 27 | + [".",".",".",".","8",".",".","7","9"] |
| 28 | +] |
| 29 | +<strong>Output:</strong> true |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p><strong>Example 2:</strong></p> |
| 33 | + |
| 34 | +<pre> |
| 35 | +<strong>Input:</strong> |
| 36 | +[ |
| 37 | + ["8","3",".",".","7",".",".",".","."], |
| 38 | + ["6",".",".","1","9","5",".",".","."], |
| 39 | + [".","9","8",".",".",".",".","6","."], |
| 40 | + ["8",".",".",".","6",".",".",".","3"], |
| 41 | + ["4",".",".","8",".","3",".",".","1"], |
| 42 | + ["7",".",".",".","2",".",".",".","6"], |
| 43 | + [".","6",".",".",".",".","2","8","."], |
| 44 | + [".",".",".","4","1","9",".",".","5"], |
| 45 | + [".",".",".",".","8",".",".","7","9"] |
| 46 | +] |
| 47 | +<strong>Output:</strong> false |
| 48 | +<strong>Explanation:</strong> Same as Example 1, except with the <strong>5</strong> in the top left corner being |
| 49 | + modified to <strong>8</strong>. Since there are two 8's in the top left 3x3 sub-box, it is invalid. |
| 50 | +</pre> |
| 51 | + |
| 52 | +<p><strong>Note:</strong></p> |
| 53 | + |
| 54 | +<ul> |
| 55 | + <li>A Sudoku board (partially filled) could be valid but is not necessarily solvable.</li> |
| 56 | + <li>Only the filled cells need to be validated according to the mentioned rules.</li> |
| 57 | + <li>The given board contain only digits <code>1-9</code> and the character <code>'.'</code>.</li> |
| 58 | + <li>The given board size is always <code>9x9</code>.</li> |
| 59 | +</ul> |
0 commit comments