Solution
- Solution is time unefficient, but memorywise beats 100%.
function isValidSudoku(board: string[][]): boolean {
for(let i = 0; i < 9; i++){
if(checkIsValid(board[i]) !== true){
return false
}
const verticalRow = []
for(let k = 0; k < 9; k++){
verticalRow.push(board[k][i])
}
if(checkIsValid(verticalRow) !== true){
return false
}
if(checkIsValid(getSuBox(board, i)) !== true){
return false
}
}
return true
};
function checkIsValid(cells: string[]): boolean {
const filled = cells.filter(cell => cell !== ".")
return (new Set(filled)).size === filled.length
}
function getSuBox(board: string[][], index: number): string[] {
const ranges = {
0: [0,1,2],
1: [3,4,5],
2: [6,7,8],
}
const hRange = ranges[index % 3];
const vRange = ranges[Math.floor((index)/3)]
return [
board[hRange[0]][vRange[0]],
board[hRange[1]][vRange[0]],
board[hRange[2]][vRange[0]],
board[hRange[0]][vRange[1]],
board[hRange[1]][vRange[1]],
board[hRange[2]][vRange[1]],
board[hRange[0]][vRange[2]],
board[hRange[1]][vRange[2]],
board[hRange[2]][vRange[2]],
]
}