Skip to content

Commit 1695ec8

Browse files
authored
Merge pull request #2408 from krishna1m/0036-valid-sudoku
Create: 0036-valid-sudoku.scala
2 parents 3f8db60 + bd6f9b3 commit 1695ec8

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

scala/0036-valid-sudoku.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
object Solution {
2+
def isValidSudoku(board: Array[Array[Char]]): Boolean = {
3+
import scala.collection.mutable.{Set => MSet}
4+
val rowSet = MSet[(Int, Char)]()
5+
val colSet = MSet[(Int, Char)]()
6+
val squareSet = MSet[(Int, Int, Char)]()
7+
8+
val indices = for {
9+
i <- (0 to 8).toList
10+
j <- (0 to 8).toList
11+
} yield (i, j)
12+
13+
indices
14+
.map { case (i, j) =>
15+
(i, j, board(i)(j))
16+
}
17+
.filter(_._3 != '.')
18+
.forall { case (i, j, char) =>
19+
if(
20+
rowSet.contains((i, char))
21+
| colSet.contains((j, char))
22+
| squareSet.contains((i / 3, j / 3, char))
23+
) false
24+
else {
25+
rowSet += ((i, char))
26+
colSet += ((j, char))
27+
squareSet += ((i / 3, j / 3, char))
28+
true
29+
}
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)