• + 0 comments

    Scala Solution using Regex

        val s: String = io.StdIn.readLine().trim
    
        val chars: Seq[String] = Seq(
          "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
          "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "x", "w", "y", "z"
        )
    
        val groupPattern: Regex = chars.map(c => s"($c{2,})").reduce(_ + "|" + _).r
    
        val compressed: String = groupPattern.findAllIn(s).toVector
          .sortBy(_.length * -1)
          .foldLeft(s){ (acc, group) =>
            acc.replace(group, s"${group.head}${group.length}")
          }
    
        println(compressed)