public class Solution {
    private final String digits = "123456789";
    private boolean isValid(char[][] board, int a, int b) {
        int i,j;
        for(i = 0; i < 9; i++) {
            if(i != a && board[i][b] == board[a][b]) {
                return false;
            }
        }

        for(j = 0; j < 9; j++) {
            if(j != b && board[a][j] == board[a][b]) {
                return false;
            }
        }

        int x = a / 3 * 3;
        int y = b / 3 * 3;
        for(i = 0; i < 3; i++) {
            for(j = 0; j< 3; j++) {
                if(x + i != a && y + j != b && board[x+i][y+j] == board[a][b]) {
                    return false;
                }
            }
        }
        return true;
    }

    private boolean dfs(char[][] board) {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (board[i][j] == '.') {
                    for (int k = 0; k < 9; k++) {
                        board[i][j] = digits.charAt(k);
                        if (isValid(board,i,j) && dfs(board)) 
                            return true;
                        board[i][j] = '.';
                    }
                    return false;
                }
            }
        }
        return true;
    }

    public void solveSudoku(char[][] board) {
        dfs(board);
    }
}
