var solveSudoku = function(board) {
const numRows = board.length;
const numCols = board[0].length;
const rows = Array.from({ length: numRows }, () => Array.from({ length: numCols + 1 }, () => 0));
const cols = Array.from({ length: numRows }, () => Array.from({ length: numCols + 1 }, () => 0));
const boxes = Array.from({ length: numRows }, () => Array.from({ length: numCols + 1 }, () => 0));
for (let row = 0; row < numRows; row++) {
for (let col = 0; col < numCols; col++) {
if (board[row][col] !== '.') {
const currentValue = parseInt(board[row][col], 10);
rows[row][currentValue]++;
cols[col][currentValue]++;
const boxId = Math.floor(row / 3) * 3 + Math.floor(col / 3);
boxes[boxId][currentValue]++;
const canPlaceNumber = (row, col, value) => {
const boxId = Math.floor(row / 3) * 3 + Math.floor(col / 3);
return rows[row][value] === 0 && cols[col][value] === 0 && boxes[boxId][value] === 0;
const backtrack = (row, col) => {
if (row === numRows - 1 && col === numCols) {
if (board[row][col] !== '.') {
return backtrack(row, col + 1);
for (let value = 1; value <= numRows; value++) {
if (!canPlaceNumber(row, col, value)) {
board[row][col] = value.toString();
const boxId = Math.floor(row / 3) * 3 + Math.floor(col / 3);
if (backtrack(row, col + 1)) {