diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 982c407..0000000 --- a/.gitignore +++ /dev/null @@ -1,6 +0,0 @@ -# Ignore everything -* - -# But not these files... -!.cs -!.gitignore \ No newline at end of file diff --git a/36.cs b/36.cs new file mode 100644 index 0000000..cf4e3f8 --- /dev/null +++ b/36.cs @@ -0,0 +1,57 @@ +public class Solution +{ + public static bool IsValidSudoku(char[][] sudokuBoard) + { + var rows = new Dictionary>(); + var columns = new Dictionary>(); + var box = new Dictionary<(int row, int col), HashSet>(); + + for (int i = 0; i < 9; i++) + { + if (!rows.ContainsKey(i)) + { + rows[i] = new HashSet(); + } + if (!columns.ContainsKey(i)) + { + columns[i] = new HashSet(); + } + } + + for (int i = 0; i < 9; i++) + { + for (int j = 0; j < 9; j++) + { + if (!box.ContainsKey((i / 3, j / 3))) + { + box[(i / 3, j / 3)] = new HashSet(); + } + } + } + + for (int i = 0; i < 9; i++) + { + for (int j = 0; j < 9; j++) + { + if (sudokuBoard[i][j] == '.') continue; + + if (IsValid(sudokuBoard, rows, columns, box, i, j)) + { + rows[i].Add(sudokuBoard[i][j]); + columns[j].Add(sudokuBoard[i][j]); + box[(i / 3, j / 3)].Add(sudokuBoard[i][j]); + } + else + { + return false; + } + } + } + return true; + } + + private static bool IsValid(char[][] sudokuBoard, Dictionary> rows, Dictionary> columns, Dictionary<(int row, int col), HashSet> box, int i, int j) + { + return !rows[i].Contains(sudokuBoard[i][j]) && !columns[j].Contains(sudokuBoard[i][j]) && !box[(i / 3, j / 3)].Contains(sudokuBoard[i][j]); + } +} \ No newline at end of file