We have to find if the given sudoku is valid, we dont have to fill the values
From the given constraints we have to check if it is satisfied, if so then we return the bool value
First approach
At first, i thought of using 2 for loops and iterating 2 the matrix.
For every row and column, we check if the given condition is satisfied, if not we return false.
Here, i forgot one thing that we need to set to store the values initially and check if it already exists in the set.
Solution
The solution is pretty straightforward.
First we store the values in a set by iterating in rows and columns
then we check the conditions by checking if the (r,val) && (val,c) is in the sett.
If all the conditions are satisfied, then we add those into the set.
At last we return True.
Code
classSolution:defisValidSudoku(self, board: List[List[str]])->bool: sett =set()for r inrange(9):for c inrange(9): val = board[r][c]if val =='.':continueif(r,val)in sett:returnFalseif(val,c)in sett:returnFalseif(r//3,c//3,val)in sett:returnFalse sett.add((r,val)) sett.add((val,c)) sett.add(r//3,r//3,val)returnTrue
Conclusion
This got me thinking for a bit, but i feel like i should have solved this all by myself. But I did learn the pattern on how to store it in a set and use if for solving constraint based problems.