CodeWars: Sudoku Solution Validator Solution
Three checksApproach
Conduct 3 checks
- row check
- column check
- square check
Implementation
1function validSolution(board) {2 const set = new Set()34 // horizontal check5 for (let row = 0; row < 9; row++) {6 for (let col = 0; col < 9; col++) {7 set.add(board[row][col])8 }910 if (set.size !== 9) return false11 set.clear()12 }1314 // vertical check15 for (let col = 0; col < 9; col++) {16 for (let row = 0; row < 9; row++) {17 set.add(board[row][col])18 }1920 if (set.size !== 9) return false21 set.clear()22 }2324 // square check25 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 }3233 if (set.size !== 9) return false34 set.clear()35 }36 }3738 return true39}
References
Comments
Loading comments...
Tags
codewars
array
matrix
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
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