-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidSudoku.java
More file actions
90 lines (77 loc) · 2.92 KB
/
Copy pathValidSudoku.java
File metadata and controls
90 lines (77 loc) · 2.92 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.HashSet;
import java.util.Set;
// My futile attempt with weird loop jumping
// class Solution {
// public boolean isValidSudoku(char[][] board) {
// Set<Character> squareSet = new HashSet<>();
// Map<Integer, Set<Character>> rowMap = new HashMap<>();
// rowLoop:
// for (int row = 0; row < board.length; row++) {
// int square = 0;
// goRightSquare:
// for (square = 0; square < 3; square++) {
// int newRow = row;
// goDown:
// for (newRow = row; newRow < 3; newRow++) {
// int collumn = 0;
// goRghtNumber:
// for (collumn = 0; collumn < 3; collumn++) {
// }
// }
// }
// row += 3;
// }
// }
// }
// Almost there
// class Solution {
// public boolean isValidSudoku(char[][] board) {
// Set<Character> rows;
// Set<Character> collumns;
// Map<String, Set<Character>> squares;
// for (int row = 0; row < board.length; row++) {
// rows = new HashSet<>();
// collumns = new HashSet<>();
// squares = new HashMap<>();
// for (int collumn = 0; collumn < board.length; collumn++) {
// if (board[row][collumn] == '.') {
// continue;
// }
// String rowKey = Integer.toString(row / 3);
// String collumnKey = Integer.toString(collumn / 3);
// String key = rowKey.concat(collumnKey);
// if (rows.contains(board[row][collumn]) || collumns.contains(board[row][collumn]))
// return false;
// if (squares.containsKey(key)) {
// if (squares.get(key).contains(board[row][collumn])) {
// return false;
// }
// }
// rows.add(board[row][collumn]);
// collumns.add(board[row][collumn]);
// if (!squares.containsKey(key)) {
// squares.put(key, new HashSet<>());
// }
// squares.get(key).add(board[row][collumn]);
// }
// }
// return true;
// }
// }
// Solution
class Solution {
public boolean isValidSudoku(char[][] board) {
Set<String> seen = new HashSet<>();
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
char number = board[i][j];
if (number != '.') {
if (!seen.add(number + " in row " + i) || !seen.add(number + " in collumn " + j) || !seen.add(number + " in suqare "+ (i/3) + "-" + (j/3))) {
return false;
}
}
}
}
return true;
}
}