Skip to content

Commit 66001d1

Browse files
committed
Solution as on 21-03-2022 10:04 pm
1 parent 497c371 commit 66001d1

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

0036. Valid Sudoku.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// 36.✅ Valid Sudoku
2+
3+
class Solution
4+
{
5+
public:
6+
bool isValid(vector<vector<char>> &board, int row, int col)
7+
{
8+
int c = board[row][col];
9+
board[row][col] = '.';
10+
for (int i = 0; i < 9; i++)
11+
if (board[row][i] == c)
12+
return false;
13+
for (int i = 0; i < 9; i++)
14+
if (board[i][col] == c)
15+
return false;
16+
for (int i = 0; i < 3; i++)
17+
for (int j = 0; j < 3; j++)
18+
if (board[row / 3 * 3 + i][col / 3 * 3 + j] == c)
19+
return false;
20+
21+
board[row][col] = c;
22+
return true;
23+
}
24+
25+
bool isValidSudoku(vector<vector<char>> &board)
26+
{
27+
28+
// Go for all the rows & columns looking for an invalid number
29+
// If you find an invalid entry, return false
30+
// If you don not find an invalid entry till the end, it has to be good, return true.
31+
for (int i = 0; i < 9; i++)
32+
{
33+
for (int j = 0; j < 9; j++)
34+
{
35+
if (board[i][j] != '.' && !isValid(board, i, j))
36+
{
37+
return false;
38+
}
39+
}
40+
}
41+
return true;
42+
}
43+
};

0 commit comments

Comments
 (0)