Skip to content

Commit 313ccfa

Browse files
solves valid sudoku
1 parent 7462b51 commit 313ccfa

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

Diff for: README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-422/2081-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-422/2081-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-442/2081-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-442/2081-1abc9c.svg)
55
![problems-solved-python](https://img.shields.io/badge/Python-186/2081-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
@@ -38,8 +38,9 @@
3838
| 29 | [Divide Two Integers](https://leetcode.com/problems/divide-two-integers) | [![Java](assets/java.png)](src/DivideTwoIntegers.java) | |
3939
| 31 | [Next Permutation](https://leetcode.com/problems/next-permutation) | [![Java](assets/java.png)](src/NextPermutation.java) | |
4040
| 33 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array) | [![Java](assets/java.png)](src/SearchInRotatedSortedArray.java) | |
41-
| 33 | [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) | |
41+
| 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) | |
43+
| 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku) | [![Java](assets/java.png)](src/ValidSudoku.java) | |
4344
| 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) | |
4445
| 53 | [Maximum SubArray](https://leetcode.com/problems/maximum-subarray) | [![Java](assets/java.png)](src/MaximumSubArray.java) [![Python](assets/python.png)](python/maximum_sum_subarray.py) | |
4546
| 58 | [Length of Last Word](https://leetcode.com/problems/length-of-last-word) | [![Java](assets/java.png)](src/LengthOfLastWord.java) [![Python](assets/python.png)](python/length_of_last_word.py) | |

Diff for: src/ValidSudoku.java

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// https://leetcode.com/problems/valid-sudoku
2+
// T: O(1)
3+
// S: O(1)
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class ValidSudoku {
9+
private static final int SUDOKU_SIZE = 9;
10+
11+
public boolean isValidSudoku(char[][] board) {
12+
if (repetitionInRows(board)) return false;
13+
if (repetitionInColumns(board)) return false;
14+
return !repetitionInBlocks(board);
15+
}
16+
17+
private boolean repetitionInRows(char[][] board) {
18+
for (char[] row : board) {
19+
if (repetitionInRow(row)) return true;
20+
}
21+
return false;
22+
}
23+
24+
private boolean repetitionInRow(char[] row) {
25+
final Set<Character> digits = new HashSet<>();
26+
for (char character : row) {
27+
if (Character.isDigit(character)) {
28+
if (digits.contains(character)) return true;
29+
digits.add(character);
30+
}
31+
}
32+
return false;
33+
}
34+
35+
private boolean repetitionInColumns(char[][] board) {
36+
final Set<Character> digits = new HashSet<>();
37+
for (int column = 0 ; column < SUDOKU_SIZE ; column++) {
38+
digits.clear();
39+
for (int row = 0 ; row < SUDOKU_SIZE ; row++) {
40+
char c = board[row][column];
41+
if (Character.isDigit(c)) {
42+
if (digits.contains(c)) return true;
43+
digits.add(c);
44+
}
45+
}
46+
}
47+
return false;
48+
}
49+
50+
private boolean repetitionInBlocks(char[][] board) {
51+
for (int i = 0 ; i < SUDOKU_SIZE / 3 ; i++) {
52+
for (int j = 0 ; j < SUDOKU_SIZE / 3 ; j++) {
53+
if (repetitionInBlock(board, i, j)) return true;
54+
}
55+
}
56+
return false;
57+
}
58+
59+
private boolean repetitionInBlock(final char[][] board, final int i, final int j) {
60+
final Set<Character> digits = new HashSet<>();
61+
for (int row = 3 * i ; row < 3 * i + 3 ; row++) {
62+
for (int column = 3 * j ; column < 3 * j + 3 ; column++) {
63+
char c = board[row][column];
64+
if (Character.isDigit(c)) {
65+
if (digits.contains(c)) return true;
66+
digits.add(c);
67+
}
68+
}
69+
}
70+
return false;
71+
}
72+
}

0 commit comments

Comments
 (0)