[HAI] isValidSudoku etalon solution by Max
parent
0a9d8b23a2
commit
69cb20429c
@ -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;
|
||||||
|
|
||||||
console.log({ cellTable, rowTable, colTable })
|
rows.add(rowKey);
|
||||||
|
colls.add(collKey);
|
||||||
|
cells.add(cellKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue