Valid Sudoku Problem Statement

You are given a 9 X 9 2D matrix named MATRIX which contains some cells filled with digits (1 to 9) and some cells left empty (denoted by 0).

Your task is to determine if there is a way to fill all the empty cells with digits (1 to 9) such that the matrix becomes a valid Sudoku solution.

Explanation:

A valid Sudoku solution must adhere to the following rules:

1. Every digit from 1 to 9 must appear exactly once in each row.
2. Every digit from 1 to 9 must appear exactly once in each column.
3. Every digit from 1 to 9 must appear exactly once in each of the nine 3x3 sub-matrices of the matrix.

Input:

T (number of test cases) 
For each test case, the input consists of 9 lines, with each line containing 9 space-separated digits (0 for an empty cell or a digit between 1 and 9 for a filled cell).

Output:

For each test case, output "yes" if a valid Sudoku solution exists, or "no" if it does not.

Example:

Input:
2
5 3 0 0 7 0 0 0 0
6 0 0 1 9 5 0 0 0
0 9 8 0 0 0 0 6 0
8 0 0 0 6 0 0 0 3
4 0 0 8 0 3 0 0 1
7 0 0 0 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9
Output:
yes
no

Constraints:

  • 1 <= T <= 5
  • N = 9
  • 0 <= MATRIX[i][j] <= 9
  • Where N denotes the size of the given square matrix.
  • Time Limit: 1 sec

Note:

1. There is always at least one empty cell (0) in the matrix.
2. The initial given matrix will always be consistent according to the Sudoku rules.
AnswerBot
1y

The task is to determine if a given 9x9 matrix can be filled with digits 1-9 to form a valid Sudoku solution.

  • Iterate through each cell in the matrix.

  • For each empty cell, try filling it with a digit fr...read more

Help your peers!
Add answer anonymously...
Amazon Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter