|
| 1 | +class Solution { |
| 2 | +public: |
| 3 | + bool isValidSudoku(vector<vector<char>>& board) { |
| 4 | + // created 3 different hashsets for rows, cols and sub-grids |
| 5 | + |
| 6 | + unordered_set<char> col[9]; |
| 7 | + unordered_set<char> row[9]; |
| 8 | + unordered_set<char> sub_grid[3][3]; |
| 9 | + |
| 10 | + for(int i=0;i<9;i++){ |
| 11 | + for(int j=0;j<9;j++){ |
| 12 | + char found=board[i][j]; |
| 13 | + |
| 14 | + // if the position is not blank , we have to check |
| 15 | + if(found != '.'){ |
| 16 | + if(row[i].count(found) || col[j].count(found) || sub_grid[i/3][j/3].count(found)) |
| 17 | + return false; |
| 18 | + } |
| 19 | + |
| 20 | + row[i].insert(found); |
| 21 | + col[j].insert(found); |
| 22 | + sub_grid[i/3][j/3].insert(found); |
| 23 | + } |
| 24 | + } |
| 25 | + return true; |
| 26 | + } |
| 27 | +}; |
0 commit comments