Meta / Amazon / Google / Microsoft Interview Question: 36. Valid Sudoku | LeetCode-36: Medium | JavaScript Implementation

Implementation:

var isValidSudoku = function (board) {
let gridSize = board.length;
const util = () => {
for (let row = 0; row < gridSize; row++) {
for (let col = 0; col < gridSize; col++) {
let countMap = {
1: false,
2: false,
3: false,
4: false,
5: false,
6: false,
7: false,
8: false,
9: false,
};
for (let i = 0; i < 9; i++) {
if (countMap[board[row][i]] && board[row][i] != ".") {
return false;
}
countMap[board[row][i]] = true;
}
countMap = {
1: false,
2: false,
3: false,
4: false,
5: false,
6: false,
7: false,
8: false,
9: false,
};
for (let i = 0; i < 9; i++) {
if (countMap[board[i][col]] && board[i][col] != ".") {
return false;
}
countMap[board[i][col]] = true;
}
let subMatrixRow = Math.floor(row / 3) * 3;
let subMatrixCol = Math.floor(col / 3) * 3;
countMap = {
1: false,
2: false,
3: false,
4: false,
5: false,
6: false,
7: false,
8: false,
9: false,
};
for (let i = subMatrixRow; i < subMatrixRow + 3; i++) {
for (let j = subMatrixCol; j < subMatrixCol + 3; j++) {
if (countMap[board[i][j]] && board[i][j] != ".") {
return false;
}
countMap[board[i][j]] = true;
}
}
}
}
return true;
};
return util();
};

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store