File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments