File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean isValidSudoku (char [][] board ) {
3
+ HashSet <Character >[] rowSet = new HashSet [9 ];
4
+ HashSet <Character >[] colSet = new HashSet [9 ];
5
+ HashSet <Character >[] boxSet = new HashSet [9 ];
6
+ for (int i = 0 ; i < 9 ; i ++) {
7
+ rowSet [i ] = new HashSet <>();
8
+ colSet [i ] = new HashSet <>();
9
+ boxSet [i ] = new HashSet <>();
10
+ }
11
+ for (int i = 0 ; i < 9 ; i ++) {
12
+ for (int j = 0 ; j < 9 ; j ++) {
13
+ if (board [i ][j ] != '.' ) {
14
+ char val = board [i ][j ];
15
+ int boxIdx = (i / 3 ) * 3 + j / 3 ;
16
+ if (rowSet [i ].contains (val ) || colSet [j ].contains (val ) || boxSet [boxIdx ].contains (val )) {
17
+ return false ;
18
+ }
19
+ rowSet [i ].add (val );
20
+ colSet [j ].add (val );
21
+ boxSet [boxIdx ].add (val );
22
+ }
23
+ }
24
+ }
25
+ return true ;
26
+ }
27
+ }
You can’t perform that action at this time.
0 commit comments