Valid Sudoku solution

    
  class Solution:
  def isValidSudoku(self, board):
      rows = [set() for _ in range(9)]
      cols = [set() for _ in range(9)]
      boxes = [set() for _ in range(9)]

      for i in range(9):
          for j in range(9):
              if board[i][j] != ".":
                  num = int(board[i][j])
                  box_idx = (i // 3) * 3 + j // 3

                  if num in rows[i] or num in cols[j] or num in boxes[box_idx]:
                      return False

                  rows[i].add(num)
                  cols[j].add(num)
                  boxes[box_idx].add(num)

      return True


Explanation (Python):

The Python solution uses sets to keep track of the numbers already seen in each row, column, and box.

It iterates through each cell of the Sudoku board using nested loops.

If the cell contains a number (not "."):

It converts the number to an integer.

Calculates the box index based on the cell's coordinates.

Checks if the number is already present in the current row, column, or box.

If it is already present, it means there is a duplicate, and the Sudoku board is invalid.

If the number is not present, it adds it to the respective row, column, and box sets.

After checking all cells, if no duplicates were found, it returns True, indicating a valid Sudoku board.


  class Solution {
    public boolean isValidSudoku(char[][] board) {
        Set[] rows = new HashSet[9];
        Set[] cols = new HashSet[9];
        Set[] boxes = new HashSet[9];

        for (int i = 0; i < 9; i++) {
            rows[i] = new HashSet<>();
            cols[i] = new HashSet<>();
            boxes[i] = new HashSet<>();
        }

        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                char num = board[i][j];
                if (num != '.') {
                    if (rows[i].contains(num) || cols[j].contains(num) || boxes[(i / 3) * 3 + j / 3].contains(num))
                        return false;

                    rows[i].add(num);
                    cols[j].add(num);
                    boxes[(i / 3) * 3 + j / 3].add(num);
                }
            }
        }

        return true;
    }
  }
         

Explanation (Java):

The Java solution follows a similar approach to the Python solution using sets to track the numbers in each row, column, and box.

It initializes three arrays of sets, rows, cols, and boxes, each containing 9 sets.

It iterates through the cells of the Sudoku board using nested loops.

If the cell contains a number (not '.'):

It checks if the number is already present in the current row, column, or box using the respective set.

If it is already present, it means there is a duplicate, and the Sudoku board is invalid.

If the number is not present, it adds it to the respective row, column, and box sets.

After checking all cells, if no duplicates were found, it returns true, indicating a valid Sudoku board.


         
  class Solution {
    public:
        bool isValidSudoku(vector>& board) {
            unordered_set seen;
    
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    char num = board[i][j];
                    if (num != '.') {
                        string row_val = "row_" + to_string(i) + "_" + num;
                        string col_val = "col_" + to_string(j) + "_" + num;
                        string box_val = "box_" + to_string(i / 3) + "_" + to_string(j / 3) + "_" + num;
    
                        if (seen.count(row_val) || seen.count(col_val) || seen.count(box_val))
                            return false;
    
                        seen.insert(row_val);
                        seen.insert(col_val);
                        seen.insert(box_val);
                    }
                }
            }
    
            return true;
        }
    };
    

Explanation (C++):

The C++ solution uses an unordered set to keep track of the seen values using unique strings as identifiers.

It iterates through the cells of the Sudoku board using nested loops.

If the cell contains a number (not '.'):

It constructs three unique strings, row_val, col_val, and box_val, representing the row, column, and box values of the current cell and number.

It checks if any of these strings are already present in the seen set.

If any of them are present, it means there is a duplicate, and the Sudoku board is invalid.

If none of the strings are present, it inserts them into the seen set.

After checking all cells, if no duplicates were found, it returns true, indicating a valid Sudoku board.

These solutions check the validity of a Sudoku board by ensuring that there are no duplicates in any row, column, or box. They use different data structures and approaches in Python, Java, and C++, but they achieve the same goal of determining the validity of the Sudoku board.

>