
Problem
Solution
Submissions
Valid Sudoku
Certification: Basic Level
Accuracy: 0%
Submissions: 0
Points: 5
Write a C program to determine if a partially filled 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits 1-9 without repetition.
- Each column must contain the digits 1-9 without repetition.
- Each of the nine 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.
Note: A Sudoku board (partially filled) could be valid but is not necessarily solvable. Only the filled cells need to be validated according to the mentioned rules. The Sudoku board is represented as a 9x9 2D array of characters where '.' represents empty cells and '1' to '9' represent filled cells.
Example 1
- Input:
board =
{
{'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'}
} - Output: true
- Explanation:
Step 1: Check all rows for duplicates. No duplicates found.
Step 2: Check all columns for duplicates. No duplicates found.
Step 3: Check all 3x3 sub-boxes for duplicates. No duplicates found.
Step 4: Since all rules are satisfied, the board is valid.
Example 2
- Input:
board =
{
{'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'}
} - Output: false
- Explanation:
Step 1: Check all rows for duplicates. Found '8' twice in the first and fourth rows.
Step 2: Since one of the rules is violated, the board is invalid.
Constraints
- board.length == 9
- board[i].length == 9
- board[i][j] is a digit (1-9) or '.'
- Time Complexity: O(1) - as the board size is fixed at 9x9
- Space Complexity: O(1) - as the board size is fixed at 9x9
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use boolean arrays to track digits seen in each row, column, and 3x3 sub-box
- Iterate through the entire board once
- For each filled cell, check if the digit has already been seen in the current row, column, or 3x3 sub-box
- If a duplicate is found, return false immediately
- If you complete the iteration without finding duplicates, return true