Valid Sudoku LeetCode Solution

Last updated on October 10th, 2024 at 12:29 am

Here, We see Valid Sudoku LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Topics

Hash Table

Companies

Apple, Snapchat, Uber

Level of Question

Medium

Valid Sudoku LeetCode Solution

Valid Sudoku LeetCode Solution

Problem Statement

Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  1. Each row must contain the digits 1-9 without repetition.
  2. Each column must contain the digits 1-9 without repetition.
  3. Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.

Note:

  • A Sudoku board (partially filled) could be valid but is not necessarily solvable.
  • Only the filled cells need to be validated according to the mentioned rules.
Input: board = 
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output: true

Example 2:

Input: board = 
[["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.

1. Valid Sudoku Leetcode Solution C++

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        int row[9][9] = {0}, col[9][9] = {0}, grid_3x3[9][9] = {0}; 
        
        for(int i = 0; i < board.size(); i++)
            for(int j = 0; j < board[i].size(); j++)
                if(board[i][j] != '.')
                {
                    int num = board[i][j] - '1', k = i/3*3+j/3; //k for finding grid index from 0 to 8
                    if(row[i][num] || col[j][num] || grid_3x3[k][num])
                        return false;
                    row[i][num] = col[j][num] = grid_3x3[k][num] = 1;
                }
        
        return true;        
    }
};

2. Valid Sudoku Leetcode Solution Java

class Solution {
    public boolean isValidSudoku(char[][] board) {
    for(int i = 0; i<9; i++){
        HashSet<Character> rows = new HashSet<Character>();
        HashSet<Character> columns = new HashSet<Character>();
        HashSet<Character> cube = new HashSet<Character>();
        for (int j = 0; j < 9;j++){
            if(board[i][j]!='.' && !rows.add(board[i][j]))
                return false;
            if(board[j][i]!='.' && !columns.add(board[j][i]))
                return false;
            int RowIndex = 3*(i/3);
            int ColIndex = 3*(i%3);
            if(board[RowIndex + j/3][ColIndex + j%3]!='.' && !cube.add(board[RowIndex + j/3][ColIndex + j%3]))
                return false;
        }
    }
    return true;    
    }
}

3. Valid Sudoku Leetcode Solution JavaScript

var isValidSudoku = function(board) {
  let rows = new Set();
  let cols = new Set();
  let boxes = new Set();
  let curRowElem;
  let curColElem;
  let curBoxElem;

  for (let i = 0; i < board.length; i += 1) {
    for (let j = 0; j < board[0].length; j += 1) {
      curRowElem = board[i][j]
      curColElem = board[j][i]
      curBoxElem = board[3 * Math.floor(i / 3) + Math.floor(j / 3)][((i * 3) % 9) + (j % 3)]

      if (rows.has(curRowElem)) return false;
      if (curRowElem !== ".") rows.add(curRowElem);

      if (cols.has(curColElem)) return false;
      if (curColElem !== ".") cols.add(curColElem);

      if (boxes.has(curBoxElem)) return false;
      if (curBoxElem !== ".") boxes.add(curBoxElem);
    }

    rows.clear()
    cols.clear()
    boxes.clear()
  }

  return true;  
};

4. Valid Sudoku Leetcode Solution Python

class Solution(object):
    def isValidSudoku(self, board):
        big = set()
        for i in xrange(0,9):
            for j in xrange(0,9):
                if board[i][j]!='.':
                    cur = board[i][j]
                    if (i,cur) in big or (cur,j) in big or (i/3,j/3,cur) in big:
                        return False
                    big.add((i,cur))
                    big.add((cur,j))
                    big.add((i/3,j/3,cur))
        return True
Scroll to Top