Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
1 public class Solution { 2 public bool IsValidSudoku(char[,] board) { 3 int rows = board.GetLength(0), cols = board.GetLength(1); 4 if (rows != 9 || cols != 9) return false; 5 6 var rused = new bool[9, 9]; 7 var cused = new bool[9, 9]; 8 var kused = new bool[9, 9]; 9 10 for (int i = 0; i < 9; i++) 11 { 12 for (int j = 0; j < 9; j++) 13 { 14 var c = board[i, j]; 15 if (c != '.') 16 { 17 var index = (int)c - (int)'1'; 18 var k = i / 3 * 3 + j / 3; 19 if (rused[i, index] || cused[j, index] || kused[k, index]) return false; 20 rused[i, index] = true; 21 cused[j, index] = true; 22 kused[k, index] = true; 23 } 24 25 } 26 } 27 28 return true; 29 } 30 }