Sudoku -Input validation fails -Can any one point out my mistake.


  • 0
    V

    public class Solution {
    public boolean isValidSudoku(char[][] board) {

      if(null == board)
      
          return false;
          
          
     for(int i =0 ; i < 9; i++) {
         
         for(int j = 0 ; j < 9 ; j++)  {
             
          /*
         it should wither be a "." or an integer 
    
       */
             
             if ( board[i][j] != '.'  && !Character.isDigit(board[i][j])) {
                  
                  return false;
              }
              
              int curr =  board[i][j] -'0';
    
      /*
         If its an integer , it must lie between 1 and 9.
     */
              
              if(curr < 1  &&  curr > 9)
              
                 return false;
    
         }
     }
          
      if(testRows(board) &&  testColumns(board) &&  testCubes(board))
         
          return true;
          
      return false;
    

    }

    private boolean testCubes(char[][] board) {

      boolean[] testVector  = new boolean[9]; 
        
     for(int n= 0 ; n<  3; n ++) 
       {
           for (int m= 0 ; m < 3; m++)  {
               
               Arrays.fill(testVector,false) ;
               
                  for(int i = n*3 ;  i < n*3 + 3; i++)  {
                      
                       for(int j = m*3; j < m*3 + 3 ; j++)  {
                           
                             if(board[i][j] == '.')
                                continue;
                             int curr = board[i][j] -'0';   
                                
                             if(testVector[curr])   
                      
                               return false;
                    
                            testVector[curr]  =true;
                            
                            
                       }
                  }
               
               
           }
       }
       
       return true;
    

    }

    private boolean testRows(char[][] board) {

     boolean[]   testVector = new boolean[9] ;
     
     for(int i = 0 ; i < 9 ; i++)  
     
             
          {
              Arrays.fill(testVector,false);
              for (int j =0 ; j < 9 ; j++)  {
                  
                   if(board[i][j] == '.')
                                continue;
                  
                  int curr = board[i][j] -'0';
                  
                  if(testVector[curr])   
                      
                      return false;
                    
                  testVector[curr]  =true;
                      
                 
             }
             
        
          }
          
          
        return true;
    

    }

    private boolean testColumns(char[][] board){

        boolean[]   testVector = new boolean[9] ;
     
     for(int i = 0 ; i < 9 ; i++)  
       
     
          {
              Arrays.fill(testVector,false);
              for (int j =0 ; j < 9 ; j++)  {
                  
                   if(board[i][j] == '.')
                                continue;
                  
                  int curr = board[j][i] -'0';
                  
                  if(testVector[curr])   
                      
                      return false;
                    
                  testVector[curr]  =true;
                      
                 
             }
          }
      
      return true;
    

    }

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.