Skip to content

Commit 5704242

Browse files
committed
added valid Sudoku solution using Set()
1 parent 7413898 commit 5704242

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
// Input:
13+
// [
14+
// ["5","3",".",".","7",".",".",".","."],
15+
// ["6",".",".","1","9","5",".",".","."],
16+
// [".","9","8",".",".",".",".","6","."],
17+
// ["8",".",".",".","6",".",".",".","3"],
18+
// ["4",".",".","8",".","3",".",".","1"],
19+
// ["7",".",".",".","2",".",".",".","6"],
20+
// [".","6",".",".",".",".","2","8","."],
21+
// [".",".",".","4","1","9",".",".","5"],
22+
// [".",".",".",".","8",".",".","7","9"]
23+
// ]
24+
// Output: true
25+
// Example 2:
26+
// Input:
27+
// [
28+
// ["8","3",".",".","7",".",".",".","."],
29+
// ["6",".",".","1","9","5",".",".","."],
30+
// [".","9","8",".",".",".",".","6","."],
31+
// ["8",".",".",".","6",".",".",".","3"],
32+
// ["4",".",".","8",".","3",".",".","1"],
33+
// ["7",".",".",".","2",".",".",".","6"],
34+
// [".","6",".",".",".",".","2","8","."],
35+
// [".",".",".","4","1","9",".",".","5"],
36+
// [".",".",".",".","8",".",".","7","9"]
37+
// ]
38+
// Output: false
39+
40+
// Explanation: Same as Example 1, except with the 5 in the top left corner being
41+
// modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
42+
// Note:
43+
44+
// A Sudoku board (partially filled) could be valid but is not necessarily solvable.
45+
// Only the filled cells need to be validated according to the mentioned rules.
46+
// The given board contain only digits 1-9 and the character '.'.
47+
// The given board size is always 9x9.
48+
49+
function isValidSudoku(board) {
50+
const isValid = validator();
51+
52+
for (let i = 0; i < 9; i++) {
53+
for (let j = 0; j < 9; j++) {
54+
if (!isValid(i, j, board[i][j])) {
55+
return false;
56+
}
57+
}
58+
}
59+
return true;
60+
}
61+
62+
const validator = () => {
63+
const horizontal = Array.from(Array(9), () => new Set());
64+
const vertical = Array.from(Array(9), () => new Set());
65+
const boxes = Array.from(Array(9), () => new Set());
66+
67+
return (row, col, value) => {
68+
if (value === '.') {
69+
return true;
70+
}
71+
72+
if (horizontal[row].has(value)) {
73+
return false;
74+
}
75+
76+
if (vertical[col].has(value)) {
77+
return false;
78+
}
79+
80+
const box = Math.floor(row / 3) * 3 + Math.floor(col / 3);
81+
if (boxes[box].has(value)) {
82+
return false;
83+
}
84+
85+
horizontal[row].add(value);
86+
vertical[col].add(value);
87+
boxes[box].add(value);
88+
return true;
89+
}
90+
}
91+
92+
const board1 = [
93+
["5", "3", ".", ".", "7", ".", ".", ".", "."],
94+
["6", ".", ".", "1", "9", "5", ".", ".", "."],
95+
[".", "9", "8", ".", ".", ".", ".", "6", "."],
96+
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
97+
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
98+
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
99+
[".", "6", ".", ".", ".", ".", "2", "8", "."],
100+
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
101+
[".", ".", ".", ".", "8", ".", ".", "7", "9"]
102+
]
103+
104+
const board2 = [
105+
["8", "3", ".", ".", "7", ".", ".", ".", "."],
106+
["6", ".", ".", "1", "9", "5", ".", ".", "."],
107+
[".", "9", "8", ".", ".", ".", ".", "6", "."],
108+
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
109+
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
110+
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
111+
[".", "6", ".", ".", ".", ".", "2", "8", "."],
112+
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
113+
[".", ".", ".", ".", "8", ".", ".", "7", "9"]
114+
]
115+
116+
console.log(isValidSudoku(board1)); // true
117+
console.log(isValidSudoku(board2)); // false

0 commit comments

Comments
 (0)