import scala.collection.mutable
object RepeatedPatternFinderInMatrix {
def findRepeatingPatterns(matrix: List[List[Int]], patternSize: Int): Unit = {
val patternMap = mutable.Map[String, mutable.Set[Int]]()
for (rowIndex <- matrix.indices) {
for (col <- 0 to matrix(rowIndex).length - patternSize) {
val pattern = matrix(rowIndex).slice(col, col + patternSize).mkString("-")
patternMap.getOrElseUpdate(pattern, mutable.Set()).add(rowIndex)
}
}
println("Repeated Patterns Found:")
for ((pattern, rows) <- patternMap if rows.size > 1) {
println(s"$pattern appears ${rows.size} times in rows: ${rows.mkString(" ")}")
}
}
def main(args: Array[String]): Unit = {
val matrix = List(
List(1, 2, 3, 8, 9, 7, 4, 9, 6),
List(1, 3, 2, 7, 8, 9, 4, 5, 6),
List(1, 2, 3, 8, 6, 1, 4, 9, 8),
List(1, 2, 3, 0, 8, 8, 4, 5, 9),
List(1, 2, 3, 4, 5, 6, 7, 8, 9),
List(1, 2, 3, 7, 0, 9, 4, 5, 7),
List(1, 3, 2, 4, 5, 6, 7, 8, 9)
)
val patternSize = 3 // Looking for sequences of 3 numbers
findRepeatingPatterns(matrix, patternSize)
}
}
/*
run:
Repeated Patterns Found:
6-7-8 appears 2 times in rows: 4 6
5-6-7 appears 2 times in rows: 4 6
7-8-9 appears 3 times in rows: 1 4 6
2-3-8 appears 2 times in rows: 0 2
9-4-5 appears 2 times in rows: 1 5
4-5-6 appears 3 times in rows: 1 4 6
1-2-3 appears 5 times in rows: 0 2 3 4 5
1-3-2 appears 2 times in rows: 1 6
*/