MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / isValidSudoku

Function isValidSudoku

javascript/0036-valid-sudoku.js:9–46  ·  view source on GitHub ↗
(board)

Source from the content-addressed store, hash-verified

7 */
8
9var 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
48/**
49 * Hash Map - Matrix

Callers

nothing calls this directly

Calls 6

getBoardsFunction · 0.85
searchGridFunction · 0.70
pushMethod · 0.45
setMethod · 0.45
getMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected