Input: board =
[["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
Constraints:
board.length == 9
board[i].length == 9
board[i][j] is a digit or '.'.
Solution
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
bool check[9] = {false};
for (const auto &row: board) {
for (const auto e: row) {
if (!isdigit(e)) continue;
if (check[e - '1']) return false;
check[e - '1'] = true;
}
memset(check, 0, 9);
}
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
const auto e = board[j][i];
if (!isdigit(e)) continue;
if (check[e - '1']) return false;
check[e - '1'] = true;
}
memset(check, 0, 9);
}
for (int k = 0; k < 9; k += 3) {
for (int i = 0; i < 9; ++i) {
if (i % 3 == 0) memset(check, 0, 9);
for (int j = k; j < k + 3; ++j) {
const auto e = board[i][j];
if (!isdigit(e)) continue;
if (check[e - '1']) return false;
check[e - '1'] = true;
}
}
}
return true;
}
};