Skip to content

Commit 56dfc02

Browse files
committed
Add Verify Sudoku Board
1 parent 712c51f commit 56dfc02

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package common.hashtable
2+
3+
class VerifySudokuBoard {
4+
5+
fun solution(board: List<List<Int>>): Boolean {
6+
// Create hash sets for each row, column, and subgrid to keep
7+
// track of numbers previously seen on any given row, column, or
8+
// subgrid.
9+
val rowSet: List<HashSet<Int>> = List(9) {
10+
hashSetOf()
11+
}
12+
val colSet: List<HashSet<Int>> = List(9) {
13+
hashSetOf()
14+
}
15+
val subBoxSet: List<List<HashSet<Int>>> = List(3) {
16+
List(3) {
17+
hashSetOf()
18+
}
19+
}
20+
for (r in 0 until 9) {
21+
for (c in 0 until 9) {
22+
val num = board[r][c]
23+
if (num == 0) {
24+
continue
25+
}
26+
// Check if 'num' has been seen in the current row,
27+
// column, or subgrid.
28+
// Use Java/Kotlin HashSet add to confirm 'num' existing status in set
29+
if (!rowSet[r].add(num)) {
30+
return false
31+
}
32+
if (!colSet[c].add(num)) {
33+
return false
34+
}
35+
if (!subBoxSet[r / 3][c / 3].add(num)) {
36+
return false
37+
}
38+
}
39+
}
40+
return true
41+
}
42+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package common.hashtable
2+
3+
import org.junit.jupiter.api.Assertions.*
4+
import org.junit.jupiter.api.Test
5+
6+
class VerifySudokuBoardTest {
7+
8+
private val question = VerifySudokuBoard()
9+
10+
@Test
11+
fun `Test case #1 - Expected True`() {
12+
val board = listOf(
13+
listOf(5, 3, 0, 0, 7, 0, 0, 0, 0),
14+
listOf(6, 0, 0, 1, 9, 5, 0, 0, 0),
15+
listOf(0, 9, 8, 0, 0, 0, 0, 6, 0),
16+
listOf(8, 0, 0, 0, 6, 0, 0, 0, 3),
17+
listOf(4, 0, 0, 8, 0, 3, 0, 0, 1),
18+
listOf(7, 0, 0, 0, 2, 0, 0, 0, 6),
19+
listOf(0, 6, 0, 0, 0, 0, 2, 8, 0),
20+
listOf(0, 0, 0, 4, 1, 9, 0, 0, 5),
21+
listOf(0, 0, 0, 0, 8, 0, 0, 7, 9)
22+
)
23+
assertTrue { question.solution(board) }
24+
}
25+
26+
@Test
27+
fun `Test case #2 - Expected False`() {
28+
val board = listOf(
29+
listOf(8, 3, 0, 0, 7, 0, 0, 0, 0),
30+
listOf(6, 0, 0, 1, 9, 5, 0, 0, 0),
31+
listOf(0, 9, 8, 0, 0, 0, 0, 6, 0),
32+
listOf(8, 0, 0, 0, 6, 0, 0, 0, 3),
33+
listOf(4, 0, 0, 8, 0, 3, 0, 0, 1),
34+
listOf(7, 0, 0, 0, 2, 0, 0, 0, 6),
35+
listOf(0, 6, 0, 0, 0, 0, 2, 8, 0),
36+
listOf(0, 0, 0, 4, 1, 9, 0, 0, 5),
37+
listOf(0, 0, 0, 0, 8, 0, 0, 7, 9)
38+
)
39+
assertFalse { question.solution(board) }
40+
}
41+
42+
43+
@Test
44+
fun `Test case #3 - Expected False`() {
45+
val board = listOf(
46+
listOf(3, 0, 6, 0, 5, 8, 4, 0, 0),
47+
listOf(5, 2, 0, 0, 0, 0, 0, 0, 0),
48+
listOf(0, 8, 7, 0, 0, 0, 0, 3, 1),
49+
listOf(1, 0, 2, 5, 0, 0, 3, 2, 0),
50+
listOf(9, 0, 0, 8, 6, 3, 0, 0, 5),
51+
listOf(0, 5, 0, 0, 9, 0, 6, 0, 0),
52+
listOf(0, 3, 0, 0, 0, 8, 2, 5, 0),
53+
listOf(0, 1, 0, 0, 0, 0, 0, 7, 4),
54+
listOf(0, 0, 5, 2, 0, 6, 0, 0, 0)
55+
)
56+
assertFalse { question.solution(board) }
57+
}
58+
}

0 commit comments

Comments
 (0)