We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 3f8db60 + bd6f9b3 commit 1695ec8Copy full SHA for 1695ec8
scala/0036-valid-sudoku.scala
@@ -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