Skip to content

Commit 8216447

Browse files
committed
feat(#36): solve the problem #36.Valid Sudoku
1 parent 0ae1431 commit 8216447

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

36.Valid Sudoku/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
2+
3+
- Each row must contain the digits 1-9 without repetition.
4+
- Each column must contain the digits 1-9 without repetition.
5+
- Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.
6+
7+
A partially filled sudoku which is valid.
8+
9+
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
10+
11+
**Example 1:**
12+
```
13+
Input:
14+
[
15+
["5","3",".",".","7",".",".",".","."],
16+
["6",".",".","1","9","5",".",".","."],
17+
[".","9","8",".",".",".",".","6","."],
18+
["8",".",".",".","6",".",".",".","3"],
19+
["4",".",".","8",".","3",".",".","1"],
20+
["7",".",".",".","2",".",".",".","6"],
21+
[".","6",".",".",".",".","2","8","."],
22+
[".",".",".","4","1","9",".",".","5"],
23+
[".",".",".",".","8",".",".","7","9"]
24+
]
25+
Output: true
26+
```
27+
**Example 2:**
28+
```
29+
Input:
30+
[
31+
  ["8","3",".",".","7",".",".",".","."],
32+
  ["6",".",".","1","9","5",".",".","."],
33+
  [".","9","8",".",".",".",".","6","."],
34+
  ["8",".",".",".","6",".",".",".","3"],
35+
  ["4",".",".","8",".","3",".",".","1"],
36+
  ["7",".",".",".","2",".",".",".","6"],
37+
  [".","6",".",".",".",".","2","8","."],
38+
  [".",".",".","4","1","9",".",".","5"],
39+
  [".",".",".",".","8",".",".","7","9"]
40+
]
41+
Output: false
42+
Explanation: Same as Example 1, except with the 5 in the top left corner being
43+
modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
44+
```
45+
**Note:**
46+
47+
- A Sudoku board (partially filled) could be valid but is not necessarily solvable.
48+
- Only the filled cells need to be validated according to the mentioned rules.
49+
- The given board contain only digits 1-9 and the character '.'.
50+
- The given board size is always 9x9.
51+
52+
**Solution:**
53+
54+
```golang
55+
import "fmt"
56+
func isValidSudoku(board [][]byte) bool {
57+
m := make(map[string]bool)
58+
for i := 0; i < 9; i++ {
59+
for j := 0; j < 9; j++ {
60+
if board[i][j] != '.' {
61+
tag := fmt.Sprintf("(%c)", board[i][j])
62+
r, c, rc := fmt.Sprintf("%d%s", i, tag), fmt.Sprintf("%s%d", tag, j), fmt.Sprintf("%d%s%d", i / 3, tag, j / 3)
63+
if m[r] || m[c] || m[rc] {
64+
return false
65+
}
66+
m[r], m[c], m[rc] = true, true, true
67+
}
68+
}
69+
}
70+
71+
return true
72+
}
73+
```

0 commit comments

Comments
 (0)