You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
674 B
TypeScript

export function isValidSudoku(board: string[][]): boolean {
const N = board.length;
const rows = new Set();
const colls = new Set();
const cells = new Set();
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
const val = board[i][j];
if (val === ".") continue;
const cellIndex = Math.floor(i / 3) * 3 + Math.floor(j / 3);
const rowKey = i + "_" + val;
const collKey = j + "_" + val;
const cellKey = cellIndex + "_" + val;
if (rows.has(rowKey) || colls.has(collKey) || cells.has(cellKey)) return false;
rows.add(rowKey);
colls.add(collKey);
cells.add(cellKey);
}
}
return true
};