Created
March 27, 2020 06:26
-
-
Save RP-3/4dc25da346a4abe9763ac220c5d3e0f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {character[][]} board | |
* @return {boolean} | |
*/ | |
var isValidSudoku = function(board) { | |
const allowedChars = new Set('123456789'); | |
const rowSets = new Array(9).fill(0).map(()=> new Set()); | |
const colSets = new Array(9).fill(0).map(()=> new Set()); | |
const sqSets = new Array(9).fill(0).map(()=> new Set()); | |
for(let row = 0; row<9; row++){ | |
for(let col = 0; col<9; col++){ | |
const sq = board[row][col]; | |
if(sq === '.') continue; | |
if(!allowedChars.has(sq)) return false; | |
if(rowSets[row].has(sq)) return false; | |
if(colSets[col].has(sq)) return false; | |
const squareIndex = Math.floor(row/3)*3 + Math.floor(col/3); | |
if(sqSets[squareIndex].has(sq)) return false; | |
rowSets[row].add(sq); | |
colSets[col].add(sq); | |
sqSets[squareIndex].add(sq); | |
} | |
} | |
return true; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment