Tutorialspoint
Problem
Solution
Submissions

Valid Sudoku

Certification: Basic Level Accuracy: 0% Submissions: 0 Points: 5

Write a C program to determine if a partially filled 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  1. Each row must contain the digits 1-9 without repetition.
  2. Each column must contain the digits 1-9 without repetition.
  3. Each of the nine 3x3 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. The Sudoku board is represented as a 9x9 2D array of characters where '.' represents empty cells and '1' to '9' represent filled 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: Check all rows for duplicates. No duplicates found.
     Step 2: Check all columns for duplicates. No duplicates found.
     Step 3: Check all 3x3 sub-boxes for duplicates. No duplicates found.
     Step 4: Since all rules are satisfied, 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: Check all rows for duplicates. Found '8' twice in the first and fourth rows.
    Step 2: Since one of the rules is violated, 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) - as the board size is fixed at 9x9
  • Space Complexity: O(1) - as the board size is fixed at 9x9
ArraysNumberKPMGAirbnb
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 boolean arrays to track digits seen in each row, column, and 3x3 sub-box
  • Iterate through the entire board once
  • For each filled cell, check if the digit has already been seen in the current row, column, or 3x3 sub-box
  • If a duplicate is found, return false immediately
  • If you complete the iteration without finding duplicates, return true

Steps to solve by this approach:

 Step 1: Create three 9x9 boolean arrays to track the occurrence of digits in each row, column, and 3x3 box.

 Step 2: Iterate through each cell of the 9x9 Sudoku board.
 Step 3: For each filled cell (non-'.'), convert the character digit to a 0-8 index.
 Step 4: Calculate the box index using the formula (row / 3) * 3 + (col / 3).
 Step 5: Check if the digit has already been seen in the current row, column, or box.
 Step 6: If a duplicate is found, return false immediately.
 Step 7: Otherwise, mark the digit as seen in the appropriate row, column, and box arrays.

Submitted Code :