valid row
valid col
valid block
class Solution { public: bool isValidSudoku(vector<vector<char> > &board) { unordered_set<int> vis; //valid row for(int i = 0 ; i < 9 ; i++) { //enumerate row vis.clear(); for(int j = 0 ; j < 9 ; j++) { if(board[i][j] == '.') continue; else { if(board[i][j] >= '1' && board[i][j] <= '9' && vis.find(board[i][j]) == vis.end()) { vis.insert(board[i][j]); } else { return false; } } } } //valid col for(int i = 0 ; i < 9 ; i++) { vis.clear(); for(int j = 0 ; j < 9 ; j++) { //i col , j = row if(board[j][i] == '.') continue; else { if(board[j][i] >= '1' && board[j][i] <= '9' && vis.find(board[j][i]) == vis.end()){ vis.insert(board[j][i]); } else { return false; } } } } //valid block for(int i = 0 ; i < 9 ; i += 3) { for(int j = 0 ; j < 9 ; j += 3) { //block start vis.clear(); for(int row = 0 ; row < 3 ; row ++) { for(int col = 0 ; col < 3 ; col ++) { if(board[i + row][j + col] == '.') continue; else { if (board[i + row][j + col] >= '1' && board[i + row][j + col] <= '9' && vis.find(board[i + row][j + col]) == vis.end()) { vis.insert(board[i + row][j + col]); } else { return false; } } } } } } return true; } };
by 1957