Skip to content

Commit 6b23658

Browse files
Create Valid Sudoku.cpp
1 parent b6b8d9e commit 6b23658

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

Medium Problems/Valid Sudoku.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//https://leetcode.com/problems/valid-sudoku/description/
2+
3+
class Solution {
4+
#define DPSolver ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
5+
public:
6+
bool checkSubThree (vector<vector<char>>& board, int row, int col){
7+
vector<bool> vis(10);
8+
for(int i = row; i < row + 3; i ++){
9+
for(int j = col; j < col + 3; j++){
10+
if(board[i][j]!='.'){
11+
if(!vis[board[i][j] - '1'])
12+
vis[board[i][j] - '1'] = true;
13+
else
14+
return false;
15+
}
16+
}
17+
}
18+
return true;
19+
}
20+
21+
bool isValidSudoku(vector<vector<char>>& board) {
22+
DPSolver;
23+
int n = 9;
24+
for (int i = 0; i < n; i++)
25+
{
26+
vector<bool> row(9, false);
27+
vector<bool> col(9, false);
28+
29+
for (int j = 0; j < 9; j++)
30+
{
31+
if (board[i][j] != '.')
32+
{
33+
if (row[board[i][j] - '1'])
34+
return false;
35+
36+
else
37+
row[board[i][j] - '1'] = true;
38+
}
39+
if (board[j][i] != '.')
40+
{
41+
if (col[board[j][i] - '1'])
42+
return false;
43+
44+
else
45+
col[board[j][i] - '1'] = true;
46+
}
47+
}
48+
}
49+
for(int i = 0 ; i < 9; i+=3)
50+
for(int j = 0; j < 9; j+=3)
51+
if(!checkSubThree(board, i,j))
52+
return false;
53+
54+
return true;
55+
}
56+
};

0 commit comments

Comments
 (0)