CodeSignal Interview – sudoku2

Problem

Sudoku is a number-placement puzzle. The objective is to fill a 9 × 9 grid with numbers in such a way that each column, each row, and each of the nine 3 × 3 sub-grids that compose the grid all contain all of the numbers from 1 to 9 one time.

Implement an algorithm that will check whether the given grid of numbers represents a valid Sudoku puzzle according to the layout rules described above. Note that the puzzle represented by grid does not have to be solvable.

Example

  • For grid = [['.', '.', '.', '1', '4', '.', '.', '2', '.'], ['.', '.', '6', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '1', '.', '.', '.', '.', '.', '.'], ['.', '6', '7', '.', '.', '.', '.', '.', '9'], ['.', '.', '.', '.', '.', '.', '8', '1', '.'], ['.', '3', '.', '.', '.', '.', '.', '.', '6'], ['.', '.', '.', '.', '.', '7', '.', '.', '.'], ['.', '.', '.', '5', '.', '.', '.', '7', '.']] the output should be
    solution(grid) = true;
  • For grid = [['.', '.', '.', '.', '2', '.', '.', '9', '.'], ['.', '.', '.', '.', '6', '.', '.', '.', '.'], ['7', '1', '.', '.', '7', '5', '.', '.', '.'], ['.', '7', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '8', '3', '.', '.', '.'], ['.', '.', '8', '.', '.', '7', '.', '6', '.'], ['.', '.', '.', '.', '.', '2', '.', '.', '.'], ['.', '1', '.', '2', '.', '.', '.', '.', '.'], ['.', '2', '.', '.', '3', '.', '.', '.', '.']] the output should be
    solution(grid) = false. The given grid is not correct because there are two 1s in the second column. Each column, each row, and each 3 × 3 subgrid can only contain the numbers 1 through 9 one time.

Solution

Idea

It turned out to be pretty simple. Use Set for easier to check and add the item. Here we just need to check/add the item in row, column and grid. Please check the code for more details.

Code

function solution(grid) {
    let seen = new Set();
    
    for (let i = 0; i < grid.length; i++) {
        for (let j = 0; j < grid.length; j++) {
            const cell = grid[i][j];
            
            if (cell === ".") {
                continue;
            }
            
            // check number appears in row
            if (seen.has(`${cell} in row ${i}`)) {
                return false;
            } else {
                seen.add(`${cell} in row ${i}`);
            }
            
            // check number appears in column
            if (seen.has(`${cell} in column ${j}`)) {
                return false;
            } else {
                seen.add(`${cell} in column ${j}`);
            }
            
            // check number appears in grid
            if (seen.has(`${cell} in grid ${Math.floor(i / 3)}-${Math.floor(j / 3)}`)) {
                return false;
            } else {
                seen.add(`${cell} in grid ${Math.floor(i / 3)}-${Math.floor(j / 3)}`);
            }
        }
    }
    return true;
}
Leave a Reply 0

Your email address will not be published. Required fields are marked *