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 a row, a column or a 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
Row 4, counting from 0, .74..89.3, has 7, 4, 8, 9 and 3 once each, and none of the other 26 groups repeats a digit either.
challenge icon

Follow-up

How would you check a 16 × 16 board with 4 × 4 boxes and the symbols 1 to 9 and A to G? What does the box formula become?

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

Test #1

Test #2

Test #3

Argument

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

Expected Output

true