File tree 1 file changed +47
-0
lines changed
1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Hash Map - Matrix
3
+ * Time O(ROWS * COLS) | Space O(ROWS * COLS)
4
+ * https://leetcode.com/problems/valid-sudoku/
5
+ * @param {character[][] } board
6
+ * @return {boolean }
7
+ */
8
+
9
+ var isValidSudoku = function ( board ) {
10
+ let row = [ ] ;
11
+ let col = [ ] ;
12
+ let squares = new Map ( ) ;
13
+ // Creating new col, row and sqaures Sets
14
+ for ( let i = 0 ; i < 9 ; i ++ ) {
15
+ let newRowSet = new Set ( ) ;
16
+ let newColSet = new Set ( ) ;
17
+ row . push ( newRowSet ) ;
18
+ col . push ( newColSet ) ;
19
+ for ( let j = 0 ; j < 9 ; j ++ ) {
20
+ squares . set ( `${ Math . floor ( i / 3 ) } :${ Math . floor ( j / 3 ) } ` , new Set ( ) ) ;
21
+ }
22
+ }
23
+
24
+ for ( let i = 0 ; i < 9 ; i ++ ) {
25
+ for ( let j = 0 ; j < 9 ; j ++ ) {
26
+ if ( board [ i ] [ j ] === '.' ) {
27
+ continue ;
28
+ }
29
+ if (
30
+ row [ i ] . has ( board [ i ] [ j ] ) ||
31
+ col [ j ] . has ( board [ i ] [ j ] ) ||
32
+ squares
33
+ . get ( `${ Math . floor ( i / 3 ) } :${ Math . floor ( j / 3 ) } ` )
34
+ . has ( board [ i ] [ j ] )
35
+ ) {
36
+ return false ;
37
+ }
38
+ row [ i ] . add ( board [ i ] [ j ] ) ;
39
+ col [ j ] . add ( board [ i ] [ j ] ) ;
40
+ squares
41
+ . get ( `${ Math . floor ( i / 3 ) } :${ Math . floor ( j / 3 ) } ` )
42
+ . add ( board [ i ] [ j ] ) ;
43
+ }
44
+ }
45
+ return true ;
46
+ } ;
47
+
1
48
/**
2
49
* Hash Map - Matrix
3
50
* Time O(ROWS * COLS) | Space O(ROWS * COLS)
You can’t perform that action at this time.
0 commit comments