# Valid Sudoku LeetCode Solution

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

## 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.
```

## 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;
}
};```Code language: PHP (php)```

## 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++){
return false;
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;
}
}```Code language: PHP (php)```

## 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 (cols.has(curColElem)) return false;

if (boxes.has(curBoxElem)) return false;
}

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

return true;
};```Code language: JavaScript (javascript)```

## 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