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

Manan Kumar Gupta
2 min readFeb 26, 2022

We will be seeing the solution to the LeetCode problem 36 in JavaScript. I hope you will find it useful.

Problem Statement:

Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

Each row must contain the digits 1–9 without repetition.
Each column must contain the digits 1–9 without repetition.
Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1–9 without repetition.
Note:

A Sudoku board (partially filled) could be valid but is not necessarily solvable.
Only the filled cells need to be validated according to the mentioned rules.

Link to Problem: https://leetcode.com/problems/wildcard-matching/

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();
};

Thank you for reading hope this code is helpful to you.

--

--

Manan Kumar Gupta

I am Frontend Developer, exploring the power of Javascript. I have worked on Angular, ReactJS and React Native.