/36. Valid Sudoku

36. Valid Sudoku

Medium
Arrays64.2% acceptance

Given a 9x9 matrix grid, where each element is a string representing either a digit from 1 to 9 or the character ., determine if the matrix represents a valid partial Sudoku configuration. A configuration is valid if: (1) Each row contains no repeated digits (excluding .), (2) Each column contains no repeated digits (excluding .), (3) Each of the nine 3x3 sub-grids contains no repeated digits (excluding .). Only filled cells (digits) need to be checked for validity. Return True if the configuration is valid, otherwise return False.

Example 1

Input: [[1,2,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.]]

Output: True

Explanation: Only two digits in the first row, no repetition.

Example 2

Input: [[1,2,3,4,5,6,7,8,9],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.]]

Output: True

Explanation: First row contains all digits 1-9, no repetition.

Example 3

Input: [[1,1,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.],[.,.,.,.,.,.,.,.,.]]

Output: False

Explanation: First row contains repeated 1.

Constraints

  • grid has exactly 9 rows.
  • Each row in grid has exactly 9 columns.
  • Each element in grid is either a digit 1-9 or ..
  • Only filled cells (digits) are validated; empty cells (.) are ignored.
Python (current runtime)

Case 1

Input: [['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.']]

Expected: True

Case 2

Input: [['1','.','.','.','.','.','.','.','.'],['.','1','.','.','.','.','.','.','.'],['.','.','1','.','.','.','.','.','.'],['.','.','.','1','.','.','.','.','.'],['.','.','.','.','1','.','.','.','.'],['.','.','.','.','.','1','.','.','.'],['.','.','.','.','.','.','1','.','.'],['.','.','.','.','.','.','.','1','.'],['.','.','.','.','.','.','.','.','1']]

Expected: True

Case 3

Input: [['1','.','.','.','.','.','.','.','.'],['1','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.']]

Expected: False

Case 4

Input: [['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['9','.','.','.','.','.','.','.','.']]

Expected: True

Case 5

Input: [['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','9']]

Expected: True