|
| 1 | +/* |
| 2 | +Valid Sudoku |
| 3 | +============ |
| 4 | +
|
| 5 | +Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: |
| 6 | +
|
| 7 | +Each row must contain the digits 1-9 without repetition. |
| 8 | +Each column must contain the digits 1-9 without repetition. |
| 9 | +Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition. |
| 10 | +Note: |
| 11 | +
|
| 12 | +A Sudoku board (partially filled) could be valid but is not necessarily solvable. |
| 13 | +Only the filled cells need to be validated according to the mentioned rules. |
| 14 | + |
| 15 | +Example 1: |
| 16 | +Input: board = |
| 17 | +[["5","3",".",".","7",".",".",".","."] |
| 18 | +,["6",".",".","1","9","5",".",".","."] |
| 19 | +,[".","9","8",".",".",".",".","6","."] |
| 20 | +,["8",".",".",".","6",".",".",".","3"] |
| 21 | +,["4",".",".","8",".","3",".",".","1"] |
| 22 | +,["7",".",".",".","2",".",".",".","6"] |
| 23 | +,[".","6",".",".",".",".","2","8","."] |
| 24 | +,[".",".",".","4","1","9",".",".","5"] |
| 25 | +,[".",".",".",".","8",".",".","7","9"]] |
| 26 | +Output: true |
| 27 | +
|
| 28 | +Example 2: |
| 29 | +Input: board = |
| 30 | +[["8","3",".",".","7",".",".",".","."] |
| 31 | +,["6",".",".","1","9","5",".",".","."] |
| 32 | +,[".","9","8",".",".",".",".","6","."] |
| 33 | +,["8",".",".",".","6",".",".",".","3"] |
| 34 | +,["4",".",".","8",".","3",".",".","1"] |
| 35 | +,["7",".",".",".","2",".",".",".","6"] |
| 36 | +,[".","6",".",".",".",".","2","8","."] |
| 37 | +,[".",".",".","4","1","9",".",".","5"] |
| 38 | +,[".",".",".",".","8",".",".","7","9"]] |
| 39 | +Output: false |
| 40 | +Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid. |
| 41 | + |
| 42 | +Constraints: |
| 43 | +board.length == 9 |
| 44 | +board[i].length == 9 |
| 45 | +board[i][j] is a digit or '.'. |
| 46 | +*/ |
| 47 | + |
| 48 | +class Solution |
| 49 | +{ |
| 50 | +public: |
| 51 | + bool valid(vector<vector<char>> &board, int r, int c, char val) |
| 52 | + { |
| 53 | + for (int i = 0; i < 9; ++i) |
| 54 | + if (board[i][c] == val || board[r][i] == val) |
| 55 | + { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + for (int i = 0; i < 3; ++i) |
| 60 | + { |
| 61 | + for (int j = 0; j < 3; ++j) |
| 62 | + { |
| 63 | + if (board[(r / 3) * 3 + i][(c / 3) * 3 + j] == val) |
| 64 | + { |
| 65 | + return false; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + bool isValidSudoku(vector<vector<char>> board) |
| 74 | + { |
| 75 | + for (int i = 0; i < board.size(); ++i) |
| 76 | + { |
| 77 | + for (int j = 0; j < board[0].size(); ++j) |
| 78 | + { |
| 79 | + |
| 80 | + if (board[i][j] != '.') |
| 81 | + { |
| 82 | + char ch = board[i][j]; |
| 83 | + board[i][j] = '!'; |
| 84 | + |
| 85 | + if (!valid(board, i, j, ch)) |
| 86 | + { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + board[i][j] = ch; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return true; |
| 96 | + } |
| 97 | +}; |
0 commit comments