CodeWars: Sudoku Solution Validator Solution

Three checks

Approach

Conduct 3 checks

  • row check
  • column check
  • square check

Implementation

1function validSolution(board) {
2 const set = new Set()
3
4 // horizontal check
5 for (let row = 0; row < 9; row++) {
6 for (let col = 0; col < 9; col++) {
7 set.add(board[row][col])
8 }
9
10 if (set.size !== 9) return false
11 set.clear()
12 }
13
14 // vertical check
15 for (let col = 0; col < 9; col++) {
16 for (let row = 0; row < 9; row++) {
17 set.add(board[row][col])
18 }
19
20 if (set.size !== 9) return false
21 set.clear()
22 }
23
24 // square check
25 for (let row = 0; row < 9; row += 3) {
26 for (let col = 0; col < 9; col += 3) {
27 for (let i = 0; i < 3; i++) {
28 for (let j = 0; j < 3; j++) {
29 set.add(board[row + i][col + j])
30 }
31 }
32
33 if (set.size !== 9) return false
34 set.clear()
35 }
36 }
37
38 return true
39}

References

Original problem

Comments

Loading comments...

Tags

codewars

array

matrix

Next Post

LeetCode: Add Digits

Could work with string instead of using math

Previous Post

LeetCode: Search in Rotated Sorted Array

Time-boxing and other solutions make life easier

HoningJS

Search Posts