From 9bdedfa9d768668c571d721d0b7a30f0eae1b58c Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Fri, 29 Aug 2025 22:19:11 -0600 Subject: 36. valid sudoku --- valid_sudoku.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ valid_sudoku.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 valid_sudoku.c create mode 100644 valid_sudoku.py diff --git a/valid_sudoku.c b/valid_sudoku.c new file mode 100644 index 0000000..dd93f60 --- /dev/null +++ b/valid_sudoku.c @@ -0,0 +1,55 @@ +// 36. Valid Sudoku +#include "leetcode.h" + +/* + * determine if a 9 x 9 sudoku board is valid. only the filled cells need to be + * validated acording to the following rules. each row must contain the digits + * 1-9 without repetition, each column must contain the digits 1-9 without + * reptition, each of the nine 3 x 3 sub-boxes of the grid must contain the + * digits 1-9 without reptition. note, a sudoku board (partially filled) could + * be valid but it is not necessarily solvable. only the filled cells need to be + * validated according to the mentioned rules. + */ + +bool isValidSudoku(char **board, int boardSize, int *boardColSize) { + bool rows[9][9] = {false}; + bool cols[9][9] = {false}; + bool boxes[9][9] = {false}; + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + if (board[i][j] != '.') { + int num = board[i][j] - '1'; + int box_idx = (i / 3) * 3 * (j / 3); + if (rows[i][num] || cols[j][num] || boxes[box_idx][num]) + return false; + rows[i][num] = cols[j][num] = boxes[box_idx][num] = true; + } + } + } + return true; +} + +int main() { + char *b1[] = {{"5", "3", ".", ".", "7", ".", ".", ".", "."}, + {"6", ".", ".", "1", "9", "5", ".", ".", "."}, + {".", "9", "8", ".", ".", ".", ".", "6", "."}, + {"8", ".", ".", ".", "6", ".", ".", ".", "3"}, + {"4", ".", ".", "8", ".", "3", ".", ".", "1"}, + {"7", ".", ".", ".", "2", ".", ".", ".", "6"}, + {".", "6", ".", ".", ".", ".", "2", "8", "."}, + {".", ".", ".", "4", "1", "9", ".", ".", "5"}, + {".", ".", ".", ".", "8", ".", ".", "7", "9"}}; + char *b2[] = {{"8", "3", ".", ".", "7", ".", ".", ".", "."}, + {"6", ".", ".", "1", "9", "5", ".", ".", "."}, + {".", "9", "8", ".", ".", ".", ".", "6", "."}, + {"8", ".", ".", ".", "6", ".", ".", ".", "3"}, + {"4", ".", ".", "8", ".", "3", ".", ".", "1"}, + {"7", ".", ".", ".", "2", ".", ".", ".", "6"}, + {".", "6", ".", ".", ".", ".", "2", "8", "."}, + {".", ".", ".", "4", "1", "9", ".", ".", "5"}, + {".", ".", ".", ".", "8", ".", ".", "7", "9"}}; + printf("%d\n", + isValidSudoku(b1, ARRAY_SIZE(b1), ARRAY_SIZE(b1[0]))); // expect: 1 + printf("%d\n", + isValidSudoku(b2, ARRAY_SIZE(b2), ARRAY_SIZE(b2[0]))); // expect: 0 +} diff --git a/valid_sudoku.py b/valid_sudoku.py new file mode 100644 index 0000000..8dd758a --- /dev/null +++ b/valid_sudoku.py @@ -0,0 +1,59 @@ +# 36. Valid Sudoku + +""" +determine if a 9 x 9 sudoku board is valid. only the filled cells need to be +validated acording to the following rules. each row must contain the digits +1-9 without repetition, each column must contain the digits 1-9 without +reptition, each of the nine 3 x 3 sub-boxes of the grid must contain the +digits 1-9 without reptition. note, a sudoku board (partially filled) could +be valid but it is not necessarily solvable. only the filled cells need to be +validated according to the mentioned rules. +""" + + +class Solution(object): + def isValidSudoku(self, board): + """ + :type board: List[List[str]] + :rtype: bool + """ + rows = [[False] * 9 for _ in range(9)] + cols = [[False] * 9 for _ in range(9)] + boxes = [[False] * 9 for _ in range(9)] + for i in range(9): + for j in range(9): + if board[i][j] != ".": + num = ord(board[i][j] - ord("1")) + box_idx = (i // 3) * 3 + (j // 3) + if rows[i][num] or cols[j][num] or boxes[box_idx][num]: + return False + rows[i][num] = cols[j][num] = boxes[box_idx][num] = True + return True + + +if __name__ == "__main__": + obj = Solution() + b1 = [ + ["5", "3", ".", ".", "7", ".", ".", ".", "."], + ["6", ".", ".", "1", "9", "5", ".", ".", "."], + [".", "9", "8", ".", ".", ".", ".", "6", "."], + ["8", ".", ".", ".", "6", ".", ".", ".", "3"], + ["4", ".", ".", "8", ".", "3", ".", ".", "1"], + ["7", ".", ".", ".", "2", ".", ".", ".", "6"], + [".", "6", ".", ".", ".", ".", "2", "8", "."], + [".", ".", ".", "4", "1", "9", ".", ".", "5"], + [".", ".", ".", ".", "8", ".", ".", "7", "9"], + ] + b2 = [ + ["8", "3", ".", ".", "7", ".", ".", ".", "."], + ["6", ".", ".", "1", "9", "5", ".", ".", "."], + [".", "9", "8", ".", ".", ".", ".", "6", "."], + ["8", ".", ".", ".", "6", ".", ".", ".", "3"], + ["4", ".", ".", "8", ".", "3", ".", ".", "1"], + ["7", ".", ".", ".", "2", ".", ".", ".", "6"], + [".", "6", ".", ".", ".", ".", "2", "8", "."], + [".", ".", ".", "4", "1", "9", ".", ".", "5"], + [".", ".", ".", ".", "8", ".", ".", "7", "9"], + ] + print(obj.isValidSudoku(b1)) + print(obj.isValidSudoku(b2)) -- cgit v1.2.3