def isValidSudoku(board):
rows = [set() for _ in range(9)]
cols = [set() for _ in range(9)]
boxes = [set() for _ in range(9)]
for i in range(9):
for j in range(9):
val = board[i][j]
if val == '.':
continue
box_index = (i // 3) * 3 + (j // 3)
if val in rows[i] or val in cols[j] or val in boxes[box_index]:
return False
rows[i].add(val)
cols[j].add(val)
boxes[box_index].add(val)
return True
# Driver code
if __name__ == '__main__':
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"]
]
print(isValidSudoku(board))
Line Notes
rows = [set() for _ in range(9)]Initialize sets to track digits in each row
box_index = (i // 3) * 3 + (j // 3)Calculate box index for current cell
if val in rows[i] or val in cols[j] or val in boxes[box_index]:Check if digit already seen in row, column, or box
rows[i].add(val)Add digit to row set after validation
import java.util.*;
public class ValidSudoku {
public static boolean isValidSudoku(char[][] board) {
Set<Character>[] rows = new HashSet[9];
Set<Character>[] cols = new HashSet[9];
Set<Character>[] boxes = new HashSet[9];
for (int i = 0; i < 9; i++) {
rows[i] = new HashSet<>();
cols[i] = new HashSet<>();
boxes[i] = new HashSet<>();
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
char val = board[i][j];
if (val == '.') continue;
int boxIndex = (i / 3) * 3 + j / 3;
if (rows[i].contains(val) || cols[j].contains(val) || boxes[boxIndex].contains(val))
return false;
rows[i].add(val);
cols[j].add(val);
boxes[boxIndex].add(val);
}
}
return true;
}
public static void main(String[] args) {
char[][] 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'}
};
System.out.println(isValidSudoku(board));
}
}
Line Notes
Set<Character>[] rows = new HashSet[9];Create array of sets for rows to track digits
int boxIndex = (i / 3) * 3 + j / 3;Compute box index for current cell
if (rows[i].contains(val) || cols[j].contains(val) || boxes[boxIndex].contains(val))Check for duplicates in row, column, or box
rows[i].add(val);Add digit to row set after confirming no duplicates
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
bool isValidSudoku(vector<vector<char>>& board) {
vector<unordered_set<char>> rows(9), cols(9), boxes(9);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
char val = board[i][j];
if (val == '.') continue;
int boxIndex = (i / 3) * 3 + j / 3;
if (rows[i].count(val) || cols[j].count(val) || boxes[boxIndex].count(val))
return false;
rows[i].insert(val);
cols[j].insert(val);
boxes[boxIndex].insert(val);
}
}
return true;
}
int main() {
vector<vector<char>> 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'}
};
cout << (isValidSudoku(board) ? "true" : "false") << endl;
return 0;
}
Line Notes
vector<unordered_set<char>> rows(9), cols(9), boxes(9);Initialize vectors of sets for rows, columns, and boxes
int boxIndex = (i / 3) * 3 + j / 3;Calculate box index for current cell
if (rows[i].count(val) || cols[j].count(val) || boxes[boxIndex].count(val))Check if digit already exists in any constraint
rows[i].insert(val);Insert digit into row set after validation
function isValidSudoku(board) {
const rows = Array.from({length: 9}, () => new Set());
const cols = Array.from({length: 9}, () => new Set());
const boxes = Array.from({length: 9}, () => new Set());
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
const val = board[i][j];
if (val === '.') continue;
const boxIndex = Math.floor(i / 3) * 3 + Math.floor(j / 3);
if (rows[i].has(val) || cols[j].has(val) || boxes[boxIndex].has(val)) {
return false;
}
rows[i].add(val);
cols[j].add(val);
boxes[boxIndex].add(val);
}
}
return true;
}
// Test
const 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']
];
console.log(isValidSudoku(board));
Line Notes
const rows = Array.from({length: 9}, () => new Set());Initialize array of sets for rows
const boxIndex = Math.floor(i / 3) * 3 + Math.floor(j / 3);Calculate box index for current cell
if (rows[i].has(val) || cols[j].has(val) || boxes[boxIndex].has(val)) {Check for duplicates in row, column, or box
rows[i].add(val);Add digit to row set after confirming no duplicates