-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0036-valid-sudoku.js
37 lines (33 loc) · 1.18 KB
/
0036-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
/**
* 36. Valid Sudoku
* https://leetcode.com/problems/valid-sudoku/
* Difficulty: Medium
*
* Determine if a 9x9 Sudoku board is valid.
* Only the filled cells need to be validated according to the following rules:
*
* - Each row must contain the digits 1-9 without repetition.
* - Each column must contain the digits 1-9 without repetition.
* - Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.
*
* The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
*/
/**
* @param {character[][]} board
* @return {boolean}
*/
var isValidSudoku = function(board) {
const rows = new Array(9).fill().map(() => new Set());
const columns = new Array(9).fill().map(() => new Set());
const boxes = new Array(9).fill().map(() => new Set());
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
const value = board[i][j];
const k = Math.floor(i / 3) * 3 + Math.floor(j / 3);
const options = [rows[i], columns[j], boxes[k]];
if (options.some(option => option.has(value))) return false;
if (value !== '.') options.forEach(option => option.add(value));
}
}
return true;
};