def is_valid_sudoku(board):
rows=[set() for _ in range(9)]; cols=[set() for _ in range(9)]; boxes=[set() for _ in range(9)]
for r in range(9):
for c in range(9):
v=board[r][c]
if v=='.': continue
b=(r//3)*3+c//3
if v in rows[r] or v in cols[c] or v in boxes[b]: return False
rows[r].add(v); cols[c].add(v); boxes[b].add(v)
return True
public boolean isValidSudoku(char[][] board){
Set[] rows=new HashSet[9],cols=new HashSet[9],boxes=new HashSet[9];
for(int i=0;i<9;i++){rows[i]=new HashSet<>();cols[i]=new HashSet<>();boxes[i]=new HashSet<>();}
for(int r=0;r<9;r++) for(int c=0;c<9;c++){
char v=board[r][c]; if(v=='.') continue;
int b=(r/3)*3+c/3;
if(!rows[r].add(v)||!cols[c].add(v)||!boxes[b].add(v)) return false;
} return true;
}
Java Set.add() returns false on dup. Python 'v in set' checks existence.
def is_valid_sudoku(board):
rows=[set() for _ in range(9)]; cols=[set() for _ in range(9)]; boxes=[set() for _ in range(9)]
for r in range(9):
for c in range(9):
v=board[r][c]
if v=='.': continue
b=(r//3)*3+c//3
if v in rows[r] or v in cols[c] or v in boxes[b]: return False
rows[r].add(v); cols[c].add(v); boxes[b].add(v)
return True
public boolean isValidSudoku(char[][] board){
Set[] rows=new HashSet[9],cols=new HashSet[9],boxes=new HashSet[9];
for(int i=0;i<9;i++){rows[i]=new HashSet<>();cols[i]=new HashSet<>();boxes[i]=new HashSet<>();}
for(int r=0;r<9;r++) for(int c=0;c<9;c++){
char v=board[r][c]; if(v=='.') continue;
int b=(r/3)*3+c/3;
if(!rows[r].add(v)||!cols[c].add(v)||!boxes[b].add(v)) return false;
} return true;
}
1. Track per row col box 2. Any dup: false