ThinkinMonke
HomeBlogsAbout
HomeBlogAbout
    Blog
    DSA Walkthrough - 07

    Table of Contents

    DSA Walkthrough - 07

    Walkthrough of the problem "Valid Sudoku"

    January 22, 2026
    2 min
    Article
    Array
    Python
    DSA Walkthrough - 07
    Algorithm

    Table of Contents

    Intro

    • 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

    class Solution:
        def isValidSudoku(self, board: List[List[str]]) -> bool:
            sett = set()
            for r in range(9):
    	        for c in range(9):
    		        val = board[r][c]
    		        
    		        if val == '.':
    			        continue
    			    if (r,val) in sett:
    				    return False
    				if (val,c) in sett:
    					return False
    				if (r//3,c//3,val) in sett:
    					return False
    				
    				sett.add((r,val))
    				sett.add((val,c))
    				sett.add(r//3,r//3,val)
    				
    		return True
    

    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.

    Published on January 22, 2026

    Estimated reading time: 2 minutes

    Share this article:

    Series: DSA Walkthrough

    Part 7 of 7
    Back to all posts

    You Might Also Like

    DSA Walkthrough - 06
    Algorithm
    1 min

    DSA Walkthrough - 06

    Walkthrough of the problem "Product of Array Except Self"

    Read more
    DSA Walkthrough - 05
    Algorithm
    2 min

    DSA Walkthrough - 05

    Walkthrough of the problem "Top K frequent Elements"

    Read more
    DSA Walkthrough - 04
    Algorithm
    2 min

    DSA Walkthrough - 04

    Walkthrough of the problem "Group Anagrams"

    Read more

    Table of Contents