I was solving leetcode and had to use defaultdict(set) for a problem. may you explain please, What defaultdict with set argument will create that normal dictionary can not ? also what is the use of set as a argument and why I used it here.
I was searching over internet about this but couldn't manage to find a precise answer. Here is the code where I used defaultdict(set),
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
rows = collections.defaultdict(set)
cols = collections.defaultdict(set)
square = collections.defaultdict(set)
for r in range(9):
for c in range(9):
if board[r][c] == ".":
continue
if (board[r][c] in rows[r] or board[r][c] in cols[c] or board[r][c] in square[(r//3, c//3)]):
return False
rows[r].add(board[r][c])
cols[c].add(board[r][c])
square[(r//3, c//3)].add(board[r][c])
return True