Menu
CoddyTech

Sudoku Solver

A sudoku board is a 9 × 9 grid divided into nine 3 × 3 boxes. On a solved board every row, every column and every box holds each digit from 1 to 9 exactly once.

The puzzle arrives as 9 strings, one per row from the top; a . is an empty cell. Fill every empty cell and return the solved board in the same form. The board has exactly one solution.

Function

solveSudoku(board: string-array) → string-array
boardstring-array
the 9 rows of the puzzle, a dot in each empty cell
Returnsstring-array
the 9 rows of the solved board

Constraints

  • board holds 9 strings of length 9.
  • Every character is . or one of the digits 1 to 9.
  • The given digits never repeat within a row, a column or a box.
  • The board has exactly one solution.

Examples

Input
board = ["9.7.....6", "824.3....", "..1..9..2", ".9...2.18", "...4....5", "1325.8...", "...687...", "......8.9", ".4.9...5."]
Output
["957214386", "824736591", "361859742", "495372618", "786491235", "132568974", "519687423", "273145869", "648923157"]
Explanation
The top row 9.7.....6 already holds 9, 7 and 6, so its six empty cells take 1, 2, 3, 4, 5 and 8. The columns and boxes decide where each one goes, and the row becomes 957214386.
challenge icon

Follow-up

In a killer sudoku, cages of cells must also add up to given sums. How would you add that rule to the search and use it to cut branches early?

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

Test #1

Argument

board = ["9.7.....6", "824.3....", "..1..9..2", ".9...2.18", "...4....5", "1325.8...", "...687...", "......8.9", ".4.9...5."]

Expected Output

["957214386", "824736591", "361859742", "495372618", "786491235", "132568974", "519687423", "273145869", "648923157"]