Leetcode 36. Valid Sudoku
Valid Sudoku – LeetCode
Can you solve this real interview question? Valid Sudoku – Determine if a 9 x 9 Sudoku board is valid.
Problem Statement
Determine if a 9 x 9
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
3 x 3
sub-boxes of the grid must contain the digits1-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.
Examples
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
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: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
Constraints
board.length == 9
board[i].length == 9
board[i][j]
is a digit1-9
or'.'
.
Solution
- Use sets to keep track of the numbers seen so far for each row, column, and 3×3 sub-box.
- Iterate through the board: Use nested loops to go through each cell in the board.
- Check constraints: For each number in the board, check if it violates the Sudoku rules by checking the corresponding row, column, and sub-box sets.
- Update sets: If a number does not violate the rules, add it to the appropriate sets.
Python:
def isValidSudoku(board):
# Use a set to keep track of the seen numbers in rows, columns, and sub-boxes
rows = [set() for _ in range(9)]
cols = [set() for _ in range(9)]
boxes = [set() for _ in range(9)]
for r in range(9):
for c in range(9):
num = board[r][c]
if num != '.':
# Calculate the index for the 3x3 sub-box
box_index = (r // 3) * 3 + (c // 3)
if num in rows[r]:
return False
rows[r].add(num)
if num in cols[c]:
return False
cols[c].add(num)
if num in boxes[box_index]:
return False
boxes[box_index].add(num)
return True
Java:
class Solution {
public boolean isValidSudoku(char[][] board) {
// Use arrays of sets to keep track of the seen numbers in rows, columns, and sub-boxes
Set<Character>[] rows = new Set[9];
Set<Character>[] cols = new Set[9];
Set<Character>[] boxes = new Set[9];
for (int i = 0; i < 9; i++) {
rows[i] = new HashSet<>();
cols[i] = new HashSet<>();
boxes[i] = new HashSet<>();
}
for (int r = 0; r < 9; r++) {
for (int c = 0; c < 9; c++) {
char num = board[r][c];
if (num != '.') {
// Calculate the index for the 3x3 sub-box
int boxIndex = (r / 3) * 3 + (c / 3);
// Check if the number is already in the respective row, column, or sub-box
if (rows[r].contains(num) || cols[c].contains(num) || boxes[boxIndex].contains(num)) {
return false;
}
// Add the number to the respective row, column, and sub-box sets
rows[r].add(num);
cols[c].add(num);
boxes[boxIndex].add(num);
}
}
}
return true;
}
}