[HAI] isValidSudoku etalon solution by Max

This commit is contained in:
2024-11-28 00:19:36 +03:00
parent 0a9d8b23a2
commit 69cb20429c
+19 -11
View File
@@ -1,17 +1,25 @@
export function isValidSudoku(board: string[][]): boolean { export function isValidSudoku(board: string[][]): boolean {
const rowTable: Record<string, string[]> = {} const N = board.length;
const colTable: Record<string, string[]> = {} const rows = new Set();
const cellTable: Record<string, string[]> = {} const colls = new Set();
const cells = new Set();
for (let i = 0; i < board.length; i++) { for (let i = 0; i < N; i++) {
rowTable[i] = board[i].filter(item => item !== "."); for (let j = 0; j < N; j++) {
if (new Set(rowTable[i]).size !== rowTable[i].length) return false; const val = board[i][j];
colTable[i] = board.map(item => item[i]).filter(item => item !== "."); if (val === ".") continue;
if (new Set(colTable[i]).size !== rowTable[i].length) return false; 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);
}
} }
console.log({ cellTable, rowTable, colTable })
return true return true
}; };