37. Sudoku Solver

Back to Homepage   |     Back to Code List


class Solution {
    public void solveSudoku(char[][] board) {
        helper(board);
    }

    private boolean helper(char[][] b) {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (b[i][j] == '.') {
                    for (char k = '1'; k <= '9'; k++) {
                        if (isValid(b, i, j, k)) {
                            b[i][j] = k;
                            if (helper(b)) return true;
                            b[i][j] = '.';
                        }
                    }
                    return false;
                }
            }
        }

        return true;
    }

    private boolean isValid(char[][] b, int row, int col, char c) {
        for (int i = 0; i < 9; i++) {
            if (b[i][col] == c) return false;
            if (b[row][i] == c) return false;
            if (b[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == c)
                return false;
        }

        return true;
    }
}