[HAI] isValidSudoku etalon solution by Max
parent
0a9d8b23a2
commit
69cb20429c
@ -1,17 +1,25 @@
|
||||
export function isValidSudoku(board: string[][]): boolean {
|
||||
const rowTable: Record<string, string[]> = {}
|
||||
const colTable: Record<string, string[]> = {}
|
||||
const cellTable: Record<string, string[]> = {}
|
||||
const N = board.length;
|
||||
const rows = new Set();
|
||||
const colls = new Set();
|
||||
const cells = new Set();
|
||||
|
||||
for (let i = 0; i < board.length; i++) {
|
||||
rowTable[i] = board[i].filter(item => item !== ".");
|
||||
if (new Set(rowTable[i]).size !== rowTable[i].length) return false;
|
||||
colTable[i] = board.map(item => item[i]).filter(item => item !== ".");
|
||||
if (new Set(colTable[i]).size !== rowTable[i].length) return false;
|
||||
}
|
||||
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;
|
||||
|
||||
console.log({ cellTable, rowTable, colTable })
|
||||
rows.add(rowKey);
|
||||
colls.add(collKey);
|
||||
cells.add(cellKey);
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue