Leetcode 36 - Valid sodoku

Note:

  • Just use brute force way is enough.
  • Don’t have to iterate once and solve the problem.
  • Save some time for other more meaningful things.

Question:

Determine if a 9 x 9 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 nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.

Note:

  • A Sudoku board (partially filled) could be valid but is not necessarily solvable.
  • Only the filled cells need to be validated according to the mentioned rules.

Example:

img

1
Output: true

Code:

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
52
53
54
55
56
57
58
59
60
/**
* @param {character[][]} board
* @return {boolean}
*/
var isValidSudoku = function (board) {
return check3x3() && checkCol() && checkRow();

function check3x3() {
let set = new Set();
let times = 0;
for (let r = 0; r <= 8; r += 3) {
for (let c = 0; c <= 8; c += 3) {
for (let i = r; i <= r + 2; i++) {
for (let j = c; j <= c + 2; j++) {
if (!isNaN(board[i][j])) {
set.add(board[i][j] - 0);
times++;
}
}
}
if (times !== set.size) return false;
times = 0;
set.clear();
}
}
return set.size === times;
}
function checkRow() {
let set = new Set();
let times = 0;
for (let i = 0; i <= 8; i++) {
for (let j = 0; j <= 8; j++) {
if (!isNaN(board[i][j])) {
set.add(board[i][j] - 0);
times++;
}
}
if (times !== set.size) return false;
times = 0;
set.clear();
}
return times === set.size;
}
function checkCol() {
let set = new Set();
let times = 0;
for (let j = 0; j <= 8; j++) {
for (let i = 0; i <= 8; i++) {
if (!isNaN(board[i][j])) {
set.add(board[i][j] - 0);
times++;
}
}
if (times !== set.size) return false;
times = 0;
set.clear();
}
return true;
}
};