Menu
CoddyTech

Valid Sudoku

MediumHashingpython iconjava iconcpp iconc iconjs icon+10

You get a 9 × 9 Sudoku board as board, a list of 9 strings with 9 characters each, one string per row. Each character is a digit from 1 to 9 or . for an empty cell. Return true if no digit appears twice in the same row, the same column or the same 3 × 3 box, and false otherwise. Only the filled cells are checked: the board does not have to be solvable.

Function

isValidSudoku(board: string-array) → boolean
boardstring-array
9 strings of 9 characters, one per row, digits 1 to 9 and . for an empty cell
Returnsboolean
true if no row, column or 3 × 3 box repeats a digit, false otherwise

Constraints

  • board.length == 9 and board[i].length == 9
  • board[i][j] is a digit from 1 to 9 or .
  • The board may be impossible to complete; only repeats among the filled cells matter.

Examples

Input
board = [".19......", "..89...3.", ".3.8.....", ".5..6....", ".74..89.3", "....7....", ".2.5..19.", "1....3...", ".8......7"]
Output
true
Explanation
Every row, column and box holds each digit at most once. Row 4 (counting from 0), .74..89.3, has 7, 4, 8, 9 and 3 once each, and the same holds for the other 26 groups, so the answer is true.

lock icon+16 hidden tests on Submit

challenge icon

Follow-up

Generalize the check to a 16 × 16 board with 4 × 4 boxes and the symbols 1 to 9 and A to G. Which numbers in your code depend on the board size, and what does the box formula become?

Reset code
def isValidSudoku(board):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

board = [".19......", "..89...3.", ".3.8.....", ".5..6....", ".74..89.3", "....7....", ".2.5..19.", "1....3...", ".8......7"]

Expected

true