-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid_sudoku.rs
75 lines (67 loc) · 2.03 KB
/
valid_sudoku.rs
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
///
/// Problem: Valid Sudoku
///
/// Determine if a `9x9` Sudoku board is **valid**.
/// Only filled cells need to be validated.
///
/// A valid Sudoku board:
/// - Each **row** contains digits `1-9` without repetition.
/// - Each **column** contains digits `1-9` without repetition.
/// - Each of the **9 sub-boxes** (`3x3` grids) contains digits `1-9` without repetition.
///
/// Note:
/// - The Sudoku board may be partially filled.
/// - The board does not need to be solvable, only valid.
///
/// Example 1:
/// Input:
/// ```plaintext
/// [
/// ["5","3",".",".","7",".",".",".","."],
/// ["6",".",".","1","9","5",".",".","."],
/// [".","9","8",".",".",".",".","6","."],
/// ["8",".",".",".","6",".",".",".","3"],
/// ["4",".",".","8",".","3",".",".","1"],
/// ["7",".",".",".","2",".",".",".","6"],
/// [".","6",".",".",".",".","2","8","."],
/// [".",".",".","4","1","9",".",".","5"],
/// [".",".",".",".","8",".",".","7","9"]
/// ]
/// ```
/// Output: `true`
///
/// Constraints:
/// - `board` is always `9x9`.
/// - Each cell is `'.'` (empty) or a digit `'1'-'9'`.
///
/// # Solution:
///
/// **Time Complexity:** `O(1)`
/// **Space Complexity:** `O(1)`
use std::collections::HashSet;
impl Solution {
pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {
let mut rows = vec![HashSet::new(); 9];
let mut cols = vec![HashSet::new(); 9];
let mut boxes = vec![HashSet::new(); 9];
for row in 0..9 {
for col in 0..9 {
let cell = board[row][col];
if cell == '.' {
continue;
}
let box_index = (row / 3) * 3 + col / 3;
if rows[row].contains(&cell)
|| cols[col].contains(&cell)
|| boxes[box_index].contains(&cell)
{
return false;
}
rows[row].insert(cell);
cols[col].insert(cell);
boxes[box_index].insert(cell);
}
}
true
}
}