-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36-valid-sudoku.js
More file actions
69 lines (53 loc) · 1.13 KB
/
Copy path36-valid-sudoku.js
File metadata and controls
69 lines (53 loc) · 1.13 KB
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
61
62
63
64
65
66
67
68
69
// https://leetcode.com/problems/valid-sudoku/
function isValidGroup(group) {
const entries = new Set()
for (const element of group) {
if (element === '.') {
continue
}
if (entries.has(element)) {
return false
}
entries.add(element)
}
return true
}
function hasInvalidLine(board) {
for (const line of board) {
if (!isValidGroup(line)) {
return true
}
}
return false
}
function hasInvalidColumn(board) {
for (let i = 0; i < 9; i++) {
const column = []
for (let j = 0; j < 9; j++) {
column.push(board[j][i])
}
if (!isValidGroup(column)) {
return true
}
}
return false
}
function hasInvalidGroup(board) {
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
const group = []
for (let k = 0; k < 3; k++) {
for (let l = 0; l < 3; l++) {
group.push(board[i * 3 + k][j * 3 + l])
}
}
if (!isValidGroup(group)) {
return true
}
}
}
return false
}
function isValidSudoku(board) {
return !hasInvalidLine(board) && !hasInvalidColumn(board) && !hasInvalidGroup(board)
}