-
Notifications
You must be signed in to change notification settings - Fork 0
/
36-validSudoku.js
113 lines (98 loc) · 3.07 KB
/
36-validSudoku.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* @param {character[][]} board
* @return {boolean}
*/
var isValidSudoku = function (board) {
const checkRows = (board) => {
for (let i = 0; i < 9; i++) {
// console.log("i", i);
const row = board[i];
// console.log("row", row);
const arr = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
for (let j = 0; j < 9; j++) {
if (row[j] !== ".") {
// console.log("row-element", row[j]);
let index = arr.indexOf(row[j]);
// console.log("row-element-index", index);
if (index !== -1) {
arr.splice(index, 1);
} else {
return false;
}
}
}
}
return true;
};
const checkColumns = (board) => {
for (let i = 0; i < 9; i++) {
// console.log("i", i);
const arr = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
for (let j = 0; j < 9; j++) {
if (board[j][i] !== ".") {
// console.log("row-element", row[j]);
let index = arr.indexOf(board[j][i]);
// console.log("row-element-index", index);
if (index !== -1) {
arr.splice(index, 1);
} else {
return false;
}
}
}
}
return true;
};
const checkSquares = (board) => {
const mapInitialCell = (i) => {
return {
row: (i % 3) * 3,
column: Math.floor(i / 3) * 3,
};
};
const mapRequiredCell = (initialCell, j) => {
const rowAddition = j % 3;
const columnAddition = Math.floor(j / 3);
return {
row: initialCell.row + rowAddition,
column: initialCell.column + columnAddition,
};
};
for (let i = 0; i < 9; i++) {
// console.log("i", i);
const arr = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
const initialCell = mapInitialCell(i);
for (let j = 0; j < 9; j++) {
const requiredCell = mapRequiredCell(initialCell, j);
if (board[requiredCell.row][requiredCell.column] !== ".") {
// console.log("row-element", row[j]);
let index = arr.indexOf(
board[requiredCell.row][requiredCell.column]
);
// console.log("row-element-index", index);
if (index !== -1) {
arr.splice(index, 1);
} else {
return false;
}
}
}
}
return true;
};
if (checkRows(board) && checkColumns(board) && checkSquares(board))
return true;
return false;
};
const board = [
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", "1", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
];
console.log(isValidSudoku(board));