Tutorialspoint
Problem
Solution
Submissions

Valid Sudoku

Certification: Basic Level Accuracy: 0% Submissions: 1 Points: 10

Write a Java program to determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
1. Each row must contain digits 1-9 without repetition.
2. Each column must contain digits 1-9 without repetition.
3. Each of the nine 3x3 sub-boxes of the grid must contain 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 rules mentioned.
- The Sudoku board is represented as a 9x9 char array where '.' represents empty cells.

Example 1
  • Input: board = [
    ["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:
    • Step 1: Create data structures to track digits in each row, column, and 3x3 sub-box.
    • Step 2: Iterate through each cell in the 9x9 Sudoku board.
    • Step 3: For each filled cell (non-'.'), check if the digit already exists in the current row.
    • Step 4: Check if the digit already exists in the current column.
    • Step 5: Check if the digit already exists in the current 3x3 sub-box.
    • Step 6: No duplicates are found in any row, column, or sub-box, so the board is valid.
Example 2
  • Input: board = [
    ["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:
    • Step 1: Create data structures to track digits in each row, column, and 3x3 sub-box.
    • Step 2: Start iterating through the Sudoku board.
    • Step 3: Notice that the first cell of the first row contains '8'.
    • Step 4: Later, in the first cell of the fourth row, we find another '8'.
    • Step 5: Since the digit '8' appears twice in the first column, this violates rule #2.
    • Step 6: Return false as the board is invalid.
Constraints
  • board.length == 9
  • board[i].length == 9
  • board[i][j] is a digit 1-9 or '.'
  • Time Complexity: O(1) - since the board size is fixed at 9x9
  • Space Complexity: O(1) - using fixed-size data structures
NumberAccentureHCL Technologies
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use three separate sets of data structures to track digits in each row, column, and 3x3 sub-box
  • You can use boolean arrays or HashSets to track seen digits
  • For the 3x3 sub-boxes, you need to map each cell (i,j) to its corresponding sub-box index
  • Skip empty cells (represented by '.')
  • Return false as soon as you encounter a violation of any rule

Steps to solve by this approach:

 Step 1: Create three 2D boolean arrays to track digits seen in each row, column, and 3x3 box.
 Step 2: Iterate through each cell in the 9x9 board.
 Step 3: Skip empty cells (represented by '.').
 Step 4: For filled cells, convert the digit to a 0-8 index for our boolean arrays.
 Step 5: Calculate the box index using the formula (i/3)*3 + j/3.
 Step 6: Check if the current digit has already been seen in the current row, column, or box.
 Step 7: If the digit has been seen, return false (invalid Sudoku); otherwise, mark the digit as seen and continue.

Submitted Code :