Is there a simpler JAVA method instead of checking each row, column and square?


  • 0
    Y

    my JAVA code was accepted. But I am looking for suggestions of simpler methods. My current code is checking each row, column and square. Put those numbers in a Hashmap, and check if any number occurred more than 1 time. If so, return false. If passed all three checking procedures, return true.

        //check each row
        for (int i=0; i<board.length ; i++){
            HashMap<Character, Integer> map = new HashMap<Character, Integer>();
            for (int j=0; j<board[i].length; j++){
                if (board[i][j] !='.'){
                    if (map.containsKey(board[i][j])){
                        map.put(board[i][j],1+map.get(board[i][j]));
                    }
                    else{
                        map.put(board[i][j], 1);
                    }
                    
                }
                
            }
            
            //check, in each row, if there is a duplicated number;
            for (int temp : map.values()){
                if (temp>1) return false;
            }
        }
        
        
        //check each column
        for (int i=0; i<board.length ; i++){
            HashMap<Character, Integer> map = new HashMap<Character, Integer>();
            for (int j=0; j<board.length; j++){
                if (board[j][i] !='.'){
                    if (map.containsKey(board[j][i])){
                        map.put(board[j][i],1+map.get(board[j][i]));
                    }
                    else{
                        map.put(board[j][i], 1);
                    }
                    
                }
                
            }
            
            //check, in each column, if there is duplicated numbers;
            for (int temp : map.values()){
                if (temp>1) return false;
            }
        }
        
        
        //check each square
        int s=0;
        while (s<9){
            HashMap<Character, Integer> map = new HashMap<Character, Integer>();
            for (int i=3*(s%3); i<3*(s%3)+3 ; i++){
            
            for (int j=3*(s/3); j<3*(s/3)+3; j++){
                if (board[i][j] !='.'){
                    if (map.containsKey(board[i][j])){
                        map.put(board[i][j],1+map.get(board[i][j]));
                    }
                    else{
                        map.put(board[i][j], 1);
                    }
                    
                }
                
            }
            
            
        }
        
        //check, in each square, if ther are duplicated numbers;
            for (int temp : map.values()){
                if (temp>1) return false;
            }
        
        s++;
        }
        return true;
        
    }
    

    }


  • 1
    R

    I have an alternate solution which is more concise.
    Basically use 3 matrices one each to check rows, columns and boxes.
    Then simply iterate over each cell and check if any of the 10 digits has already appeared in the corresponding box, row or column.

    public class Solution {

    public boolean isValidSudoku(char[][] board) {
        // can simply use boolean[][]
        char [][]rowCheck = new char[9][10];
        char [][]colCheck = new char[9][10];
        char [][]boxCheck = new char[9][10];
        for (int i = 0; i < 9; ++i) {
            for (int j = 0; j < 9; ++j) {
                char charToCheck = board[i][j];
                if (charToCheck != '.') {
                    int box = (i/3) * 3 + (j/3); // find the corresponding box index
                    if (boxCheck[box][charToCheck - '0'] == 1) return false;
                    boxCheck[box][charToCheck - '0'] = 1;
                    if (rowCheck[i][charToCheck - '0'] == 1) return false;
                    rowCheck[i][charToCheck - '0'] = 1;
                    if (colCheck[j][charToCheck - '0'] == 1) return false;
                    colCheck[j][charToCheck - '0'] = 1;
                }
            }
        }
        return true;
    }
    

    }


  • 9
    G

    Doing it all at the same time

    public class Solution {
    public boolean isValidSudoku(char[][] board) {
        HashSet[] row  = new HashSet[9];
        HashSet[] col  = new HashSet[9];
        HashSet[] area = new HashSet[9];
        for (int i = 0; i < 9; i++) {
            row[i]  = new HashSet<Character>();
            col[i]  = new HashSet<Character>();
            area[i] = new HashSet<Character>();
        }
        
        for (int i = 0; i < 9; i++) { //rows
            for (int j = 0; j < 9; j++) { //cols
                if (board[i][j] != '.') {
                    int nArea = (3 * (i  / 3)) + (j  / 3);
                    if (row[i].contains(board[i][j]) || 
                        col[j].contains(board[i][j]) || 
                        area[nArea].contains(board[i][j]))
                        return false;
                    
                    row[i].add(board[i][j]);
                    col[j].add(board[i][j]);
                    area[nArea].add(board[i][j]);
                }
            }
        }
        
        return true;
    }
    

    }


Log in to reply
 

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