Sloved problem in c++ #81
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Solved #51
Problem: 36. Valid Sudoku
// Approach: Hashing with Bitsets
// Implementation Idea
// Use 3 arrays of bitsets (or boolean sets):
// row[9] → track used digits in each row
// col[9] → track used digits in each column
// block[9] → track used digits in each 3×3 block
//
// Traverse the board:
// - Skip if cell is '.'
// - Convert digit '1'–'9' → index 0–8
// - If digit already exists in row/col/block → invalid
// - Else, mark it as used in all three
// If traversal completes with no conflicts → valid board.
// Block index formula: (r / 3) * 3 + (c / 3)
// Intuition
// Think of it as tracking duplicates across three dimensions:
// - Row-wise
// - Column-wise
// - Block-wise
// As we read each cell, we “register” that digit in its row, column, and block.
// If any duplicate appears, it violates Sudoku rules.
// Bitsets make checks and updates O(1) — effectively running 3 hash lookups in parallel.
Complexity
// Time Complexity: O(9×9) = O(1)
// Space Complexity: O(9×3) = O(1)
Code Implementation
class Solution {
public:
bool isValidSudoku(vector<vector>& board) {
vector<bitset<9>> row(9), col(9), block(9);
};