Hackerrank – String Compression
Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution Look at the implementation. The steps and the result are described in problem statement. I created solution in: Scala All solutions are also available on my GitHub. Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
object Solution extends App { val line = Source.stdin.getLines().toList.head val subs = uniformSubstrings(line) val substringCompressed = subs.map(s => { val length = s.length if(length > 1) { s.head.toString + length } else { s.head.toString } }) println(substringCompressed.mkString("")) def uniformSubstrings(s: String): List[String] = { if(s.length <= 0) { return Nil } val c = s.charAt(0) val substring = s.takeWhile(_ == c) substring :: uniformSubstrings(s.drop(substring.length)) } } |