forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0036-valid-sudoku.cs
30 lines (24 loc) · 1.08 KB
/
0036-valid-sudoku.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Solution {
public bool IsValidSudoku(char[][] board) {
var rows = new Dictionary<int, HashSet<char>>();
var cols = new Dictionary<int, HashSet<char>>();
var squares = new Dictionary<(int, int), HashSet<char>>();
for(var r = 0; r < 9; r++) {
rows[r] = new HashSet<char>();
for(var c = 0; c < 9; c++) {
if(!cols.ContainsKey(c)) cols[c] = new HashSet<char>();
if(!squares.ContainsKey((r/3, c/3)))
squares[(r/3, c/3)] = new HashSet<char>();
if(board[r][c] == '.') continue;
if(rows[r].Contains(board[r][c]) ||
cols[c].Contains(board[r][c]) ||
squares[(r / 3, c / 3)].Contains(board[r][c]))
return false;
rows[r].Add(board[r][c]);
cols[c].Add(board[r][c]);
squares[(r / 3, c / 3)].Add(board[r][c]);
}
}
return true;
}
}