Getting TLE. Can anyone please help where I am getting wrong?


#1
bool isVal(int row, int col, int num, vector<vector<char>> &A){
    //check square
    if(row >= 0 && row < 3){
        if(col >= 0 && col < 3){
            for(int i = 0; i < 3; i++){
                for(int j = 0; j < 3; j++){
                    if(isdigit(A[i][j]) && A[i][j] - '0' == num)return false;
                }
            }
        
        }
        if( col >= 3 && col < 6){
            for(int i = 0; i < 3; i++){
                for(int j = 3; j < 6; j++){
                    if(isdigit(A[i][j]) && A[i][j] - '0' == num)return false;
                }
            }
        }
        if(col >= 6 && col < 9){
            for(int i = 0; i < 3; i++){
                for(int j = 6; j < 9; j++){
                    if(isdigit(A[i][j]) && A[i][j] - '0' == num)return false;
                }
            }
        }
    }
    if( row >= 3 && row < 6){
        if(col >= 0 && col < 3){
            for(int i = 3; i < 6; i++){
                for(int j = 0; j < 3; j++){
                    if(isdigit(A[i][j]) && A[i][j] - '0' == num)return false;
                }
            }
        }
        if( col >= 3 && col < 6){
            for(int i = 3; i < 6; i++){
                for(int j = 3; j < 6; j++){
                    if(isdigit(A[i][j]) && A[i][j] - '0' == num)return false;
                }
            }
        }
        if(col >= 6 && col < 9){
            for(int i = 3; i < 6; i++){
                for(int j = 6; j < 9; j++){
                    if(isdigit(A[i][j]) && A[i][j] - '0' == num)return false;
                }
            }
        }
    }
    if(row >= 6 && row < 9){
        if(col >= 0 && col < 3){
            for(int i = 6; i < 9; i++){
                for(int j = 0; j < 3; j++){
                    if(isdigit(A[i][j]) && A[i][j] - '0' == num)return false;
                }
            }
        }
        if( col >= 3 && col < 6){
            for(int i = 6; i < 9; i++){
                for(int j = 3; j < 6; j++){
                    if(isdigit(A[i][j]) && A[i][j] - '0' == num)return false;
                }
            }
        }
        if(col >= 6 && col < 9){
            for(int i = 6; i < 9; i++){
                for(int j = 6; j < 9; j++){
                    if(isdigit(A[i][j]) && A[i][j] - '0' == num)return false;
                }
            }
        }
    }
    //check row
    for(int i = 0; i < A[row].size(); i++){
        if(isdigit(A[row][i]) && A[row][i]-'0' == num)return false;
    }
    //check col
    for(int i = 0; i < A[col].size(); i++){
        if(isdigit(A[i][col]) && A[i][col]-'0' == num)return false;
    }
    return true;
}
void solve(vector<vector<char>>& A, int row, int col){
    if(row == A.size()){
        return; 
    }
    if(col == A.size()){
        solve(A, row+1, 0);
        return;
    }

    for(int i = col; i < A[row].size(); i++){
        if(!isdigit(A[row][i]-'0')){
            bool exists = false;
            for(int num = 1; num < 10; i++){
                if(isVal(row, i, num, A)){
                    exists = true;
                    A[row][i] = '0'+num;
                    solve(A, row, col+1);
                    A[row][i] = '.';
                }
            }
            if(!exists) return;
        }else{
            solve(A, row, col+1);
        }
        
    }
}
void Solution::solveSudoku(vector<vector<char> > &A) {
    // Do not write main() function.
    // Do not read input, instead use the arguments to the function.
    // Do not print the output, instead return values as specified
    // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
    solve(A, 0, 0);
}