Java solution using HashSet


  • 0
    C

    public boolean isValidSudoku(char[][] board) {
    Set<Character>[] rows = new HashSet[9];
    Set<Character>[] columns = new HashSet[9];
    Set<Character>[] blocks = new HashSet[9];

        for (int i = 0; i < 9; i++) {
            rows[i] = new HashSet<>();
            columns[i] = new HashSet<>();
            blocks[i] = new HashSet<>();
        }
    
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                char val = board[i][j];
                if (val != '.' && (!rows[i].add(val) || !columns[j].add(val) || !blocks[i/3 + j/3*3].add(val))) {
                  return false;
                }
            }
        }
        return true;
    }

Log in to reply
 

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