lc-go/solutions/0/q36/solution.go
2025-12-23 18:39:11 +09:00

63 lines
1 KiB
Go

package q36
func validateCol(board [][]byte, col int) bool {
var present [10]bool
for i := range 9 {
c := board[i][col]
if c != '.' {
if present[c-'0'] {
return false
}
present[c-'0'] = true
}
}
return true
}
func validateRow(board [][]byte, row int) bool {
var present [10]bool
for i := range 9 {
c := board[row][i]
if c != '.' {
if present[c-'0'] {
return false
}
present[c-'0'] = true
}
}
return true
}
func validateBox(board [][]byte, box int) bool {
xOffset := (box % 3) * 3
yOffset := (box / 3) * 3
var present [10]bool
for i := range 9 {
x := xOffset + i%3
y := yOffset + i/3
c := board[y][x]
if c != '.' {
if present[c-'0'] {
return false
}
present[c-'0'] = true
}
}
return true
}
func validate(board [][]byte) bool {
for i := range 9 {
if !validateCol(board, i) || !validateRow(board, i) || !validateBox(board, i) {
return false
}
}
return true
}
func isValidSudoku(board [][]byte) bool {
return validate(board)
}
var _ = isValidSudoku