-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path36.valid-sudoku.js
51 lines (46 loc) · 1.11 KB
/
36.valid-sudoku.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* @lc app=leetcode id=36 lang=javascript
*
* [36] Valid Sudoku
*/
// @lc code=start
/**
* @param {character[][]} board
* @return {boolean}
*/
var isValidSudoku = function (board) {
if (board.length !== 9) return false;
for (const row of board) {
if (!isValidSet(row)) return false;
if (row.length !== 9) return false;
}
for (let i = 0; i < board.length; i++) {
const testColumn = [];
for (let j = 0; j < board.length; j++) {
testColumn.push(board[j][i]);
}
if (!isValidSet(testColumn)) return false;
}
for (let i = 0; i < board.length; i += 3) {
for (let j = 0; j < board.length; j += 3) {
const testSquare = [
...board[i].slice(j, j + 3),
...board[i + 1].slice(j, j + 3),
...board[i + 2].slice(j, j + 3),
];
if (!isValidSet(testSquare)) return false;
}
}
return true;
};
function isValidSet(arr) {
const map = new Set();
for (const el of arr) {
if (el > 9 || el < 1 || map.has(el)) return false;
if (el === ".") continue;
map.add(el);
}
if (map.length > 9) return false;
return true;
}
// @lc code=end