Skip to content

Commit

Permalink
Create: 0036-valid-sudoku.scala
Browse files Browse the repository at this point in the history
  • Loading branch information
krishna1m committed Apr 12, 2023
1 parent 050c9c5 commit bd6f9b3
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions scala/0036-valid-sudoku.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
object Solution {
def isValidSudoku(board: Array[Array[Char]]): Boolean = {
import scala.collection.mutable.{Set => MSet}
val rowSet = MSet[(Int, Char)]()
val colSet = MSet[(Int, Char)]()
val squareSet = MSet[(Int, Int, Char)]()

val indices = for {
i <- (0 to 8).toList
j <- (0 to 8).toList
} yield (i, j)

indices
.map { case (i, j) =>
(i, j, board(i)(j))
}
.filter(_._3 != '.')
.forall { case (i, j, char) =>
if(
rowSet.contains((i, char))
| colSet.contains((j, char))
| squareSet.contains((i / 3, j / 3, char))
) false
else {
rowSet += ((i, char))
colSet += ((j, char))
squareSet += ((i / 3, j / 3, char))
true
}
}
}
}

0 comments on commit bd6f9b3

Please sign in to comment.