From f944fc6b5129a6e6e62381267af1f4ee27c26403 Mon Sep 17 00:00:00 2001 From: Mauricio Date: Wed, 25 Oct 2023 00:47:29 -0400 Subject: [PATCH 1/2] Solution for Valid-Sudoku --- .gitignore | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 .gitignore 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 From 6e7d61c8b5fe5d72d0dde2bf2ae642c2927ec5c8 Mon Sep 17 00:00:00 2001 From: Mauricio Date: Wed, 25 Oct 2023 00:48:13 -0400 Subject: [PATCH 2/2] Solution for Valid-Sudoku --- 36.cs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 36.cs 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