-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0036_valid_sudoku.go
More file actions
80 lines (69 loc) · 1.86 KB
/
s0036_valid_sudoku.go
File metadata and controls
80 lines (69 loc) · 1.86 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
70
71
72
73
74
75
76
77
78
79
80
/*
https://leetcode.com/problems/valid-sudoku/
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.
*/
package solutions
const dot = '.'
func isValidSudoku(board [][]byte) bool {
subBoxes := make(map[int][]byte, 9)
rows := make(map[int][]byte, 9)
cols := make(map[int][]byte, 9)
for i := 0; i < len(board); i++ {
rows[i] = board[i]
for j := 0; j < len(board[i]); j++ {
cols[j] = append(cols[j], board[i][j])
if i < 3 {
if j < 3 {
subBoxes[1] = append(subBoxes[1], board[i][j])
} else if j < 6 {
subBoxes[2] = append(subBoxes[2], board[i][j])
} else {
subBoxes[3] = append(subBoxes[3], board[i][j])
}
} else if i < 6 {
if j < 3 {
subBoxes[4] = append(subBoxes[4], board[i][j])
} else if j < 6 {
subBoxes[5] = append(subBoxes[5], board[i][j])
} else {
subBoxes[6] = append(subBoxes[6], board[i][j])
}
} else {
if j < 3 {
subBoxes[7] = append(subBoxes[7], board[i][j])
} else if j < 6 {
subBoxes[8] = append(subBoxes[8], board[i][j])
} else {
subBoxes[9] = append(subBoxes[9], board[i][j])
}
}
}
}
if !isMapValid(rows) || !isMapValid(cols) || !isMapValid(subBoxes) {
return false
}
return true
}
func isMapValid(xs map[int][]byte) bool {
for _, r := range xs {
m := make(map[byte]bool)
for _, e := range r {
if e != dot {
if m[e] {
return false
}
m[e] = true
}
}
}
return true
}