Code Logo

Valid Sudoku

Published at24 Jul 2026
Medium 0 views
Like0

Determine if a 9x9 Sudoku board is valid. A valid board has digits 1-9 appearing at most once per row, per column, and per 3x3 sub-box. Empty cells are marked with '.'. The board may be partially filled — only filled cells must follow the rules.

A brute force approach would scan each row, column, and box separately, requiring three passes over the board. A hash map approach solves it in one pass: as we iterate each cell, we track its digit in three concurrent hash maps — one for the cell's row, one for its column, and one for its 3x3 box. If any digit already exists in the corresponding map, the board is invalid.

The 3x3 box index can be computed from the cell coordinates: boxIndex = floor(row / 3) * 3 + floor(col / 3). This formula maps each cell to one of nine boxes numbered 0 through 8. The algorithm runs in O(81) = O(1) time since the board is always 9x9, and O(1) space (nine hash maps each with at most 9 entries).

Edge cases include an empty board (all '.' — valid), a board with only one row/column conflict, and a board with a box conflict that is not visible in any row or column scan. The one-pass approach catches all three simultaneously.

Example Input & Output

Example 1
Input
[["5", "3", ".", ".", "7", ".", ".", ".", "."], ["6", ".", ".", "1", "9", "5", ".", ".", "."], [".", "9", "8", ".", ".", ".", ".", "6", "."], ["8", ".", ".", ".", "6", ".", ".", ".", "3"], ["4", ".", ".", "8", ".", "3", ".", ".", "1"], ["7", ".", ".", ".", "2", ".", ".", ".", "6"], [".", "6", ".", "5", ".", ".", "2", "8", "."], ["6", ".", ".", "4", "1", "9", ".", ".", "5"], [".", ".", ".", ".", "8", ".", ".", "7", "9"]]
Output
false
Explanation

Bottom-left box has two 6s: row 6 col 1 and row 7 col 0.

Example 2
Input
[["5","5",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
Output
false
Explanation

Duplicate 5 in first row.

Example 3
Input
[[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."]]
Output
true
Explanation

Empty board is valid.

Example 4
Input
[["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
Output
true
Explanation

Valid board with no conflicts.

Example 5
Input
[["8","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
Output
false
Explanation

Duplicate 8 in first column.

Algorithm Flow

Recommendation Algorithm Flow for Valid Sudoku
Recommendation Algorithm Flow for Valid Sudoku

Solution Approach

Create three arrays of 9 hash maps for rows, columns, and boxes. Iterate each cell, compute box index, check for duplicates across all three maps.

function solution(board) {
  var rows = [{},{},{},{},{},{},{},{},{}];
  var cols = [{},{},{},{},{},{},{},{},{}];
  var boxes = [{},{},{},{},{},{},{},{},{}];
  for (var i = 0; i < 9; i++) {
    for (var j = 0; j < 9; j++) {
      var ch = board[i][j];
      if (ch === '.') continue;
      var b = Math.floor(i / 3) * 3 + Math.floor(j / 3);
      if (rows[i][ch] || cols[j][ch] || boxes[b][ch]) return false;
      rows[i][ch] = 1; cols[j][ch] = 1; boxes[b][ch] = 1;
    }
  }
  return true;
}

Time O(1), Space O(1) — fixed 9x9 board.

Best Answers

java
import java.util.*;
class Solution {
    public boolean solution(char[][] board) {
        Map<Integer,Set<Character>> rows = new HashMap<>();
        Map<Integer,Set<Character>> cols = new HashMap<>();
        Map<Integer,Set<Character>> boxes = new HashMap<>();
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                char ch = board[i][j];
                if (ch == '.') continue;
                int b = (i / 3) * 3 + j / 3;
                if (rows.computeIfAbsent(i, k -> new HashSet<>()).contains(ch) ||
                    cols.computeIfAbsent(j, k -> new HashSet<>()).contains(ch) ||
                    boxes.computeIfAbsent(b, k -> new HashSet<>()).contains(ch)) return false;
                rows.get(i).add(ch);
                cols.get(j).add(ch);
                boxes.get(b).add(ch);
            }
        }
        return true;
    }
}