Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit aa6caf7

Browse files
solves sudoku solver in java
1 parent 8fea23e commit aa6caf7

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
| 34 | [Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array) | [![Java](assets/java.png)](src/FindFirstAndLastPositionOfElementInSortedArray.java) | |
4242
| 35 | [Search Inserted Position](https://leetcode.com/problems/search-insert-position/) | [![Java](assets/java.png)](src/SearchInsertPosition.java) [![Python](assets/python.png)](python/search_insert_position.py) | |
4343
| 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku) | [![Java](assets/java.png)](src/ValidSudoku.java) | |
44+
| 37 | [Sudoku Solver](https://leetcode.com/problems/sudoku-solver) | [![Java](assets/java.png)](src/SudokuSolver.java) | |
4445
| 38 | [Count and Say](https://leetcode.com/problems/count-and-say) | [![Java](assets/java.png)](src/CountAndSay.java) [![Python](assets/python.png)](python/count_and_say.py) | |
4546
| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum) | [![Java](assets/java.png)](src/CombinationSum.java) | |
4647
| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii) | [![Java](assets/java.png)](src/CombinationSumII.java) | |

src/SudokuSolver.java

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// https://leetcode.com/problems/sudoku-solver
2+
// T: O(n^2 * k) but n = 9 and k = 9 so just O(1)
3+
// S: O(n^2) but n = 9 so just O(1)
4+
5+
import java.util.HashMap;
6+
import java.util.HashSet;
7+
import java.util.Map;
8+
import java.util.Set;
9+
10+
public class SudokuSolver {
11+
private record Position(int row, int column) {
12+
private static final Position start = new Position(0, 0);
13+
14+
private boolean isValid() {
15+
return row >= 0
16+
&& row < 9
17+
&& column >= 0
18+
&& column < 9;
19+
}
20+
21+
private Position next() {
22+
if (column == 8) return new Position(row + 1, 0);
23+
return new Position(row, column + 1);
24+
}
25+
}
26+
27+
private static final Map<Integer, Set<Integer>> rows = new HashMap<>();
28+
private static final Map<Integer, Set<Integer>> columns = new HashMap<>();
29+
private static final Map<Integer, Set<Integer>> subGrids = new HashMap<>();
30+
31+
public static void solveSudoku(char[][] board) {
32+
computeDigitsInRows(board);
33+
computeDigitsInColumns(board);
34+
computeDigitsInSubGrids(board);
35+
solveSudoku(board, Position.start);
36+
}
37+
38+
private static boolean solveSudoku(char[][] board, Position position) {
39+
if (!position.isValid()) return true;
40+
if (isAlreadyFilled(board, position)) return solveSudoku(board, position.next());
41+
42+
for (int i = 1 ; i <= 9 ; i++) {
43+
if (isInRow(i, position) || isInColumn(i, position) || isInSubGrid(i, position)) continue;
44+
insertInGrid(i, position, board);
45+
boolean solved = solveSudoku(board, position.next());
46+
if (solved) return true;
47+
removeFromGrid(i, position, board);
48+
}
49+
return false;
50+
}
51+
52+
private static boolean isAlreadyFilled(char[][] board, Position position) {
53+
return board[position.row][position.column] != '.';
54+
}
55+
56+
private static boolean isInRow(int digit, Position position) {
57+
return rows.get(position.row).contains(digit);
58+
}
59+
60+
private static boolean isInColumn(int digit, Position position) {
61+
return columns.get(position.column).contains(digit);
62+
}
63+
64+
private static boolean isInSubGrid(int digit, Position position) {
65+
return subGrids.get(getSubGridId(position)).contains(digit);
66+
}
67+
68+
private static void insertInGrid(int digit, Position position, char[][] board) {
69+
rows.get(position.row).add(digit);
70+
columns.get(position.column).add(digit);
71+
subGrids.get(getSubGridId(position)).add(digit);
72+
board[position.row][position.column] = toChar(digit);
73+
}
74+
75+
private static void removeFromGrid(int digit, Position position, char[][] board) {
76+
rows.get(position.row).remove(digit);
77+
columns.get(position.column).remove(digit);
78+
subGrids.get(getSubGridId(position)).remove(digit);
79+
board[position.row][position.column] = '.';
80+
}
81+
82+
private static void computeDigitsInRows(char[][] board) {
83+
for (int row = 0 ; row < 9 ; row++) {
84+
final Set<Integer> digits = new HashSet<>();
85+
for (char element : board[row]) {
86+
if (Character.isDigit(element)) {
87+
digits.add(toInt(element));
88+
}
89+
}
90+
rows.put(row, digits);
91+
}
92+
}
93+
94+
private static void computeDigitsInColumns(char[][] board) {
95+
for (int column = 0 ; column < 9 ; column++) {
96+
final Set<Integer> digits = new HashSet<>();
97+
for (int row = 0 ; row < 9 ; row++) {
98+
if (Character.isDigit(board[row][column])) {
99+
digits.add(toInt(board[row][column]));
100+
}
101+
}
102+
columns.put(column, digits);
103+
}
104+
}
105+
106+
private static void computeDigitsInSubGrids(char[][] board) {
107+
for (int i = 0 ; i < 3 ; i++) {
108+
for (int j = 0 ; j < 3 ; j++) {
109+
final int subGridId = getSubGridId(i, j);
110+
final Set<Integer> digits = new HashSet<>();
111+
for (int row = 3 * i ; row < 3 * i + 3 ; row++) {
112+
for (int column = 3 * j ; column < 3 * j + 3 ; column++) {
113+
if (Character.isDigit(board[row][column])) {
114+
digits.add(toInt(board[row][column]));
115+
}
116+
}
117+
}
118+
subGrids.put(subGridId, digits);
119+
}
120+
}
121+
}
122+
123+
private static int toInt(char c) {
124+
return c - '0';
125+
}
126+
127+
private static char toChar(int digit) {
128+
return (char) ('0' + digit);
129+
}
130+
131+
private static int getSubGridId(int i, int j) {
132+
return 10 * i + j;
133+
}
134+
135+
private static int getSubGridId(Position position) {
136+
int i = position.row / 3;
137+
int j = position.column / 3;
138+
return getSubGridId(i, j);
139+
}
140+
}

0 commit comments

Comments
 (0)