Skip to content

Instantly share code, notes, and snippets.

@maasg
Last active December 22, 2015 13:38
Show Gist options
  • Select an option

  • Save maasg/6479884 to your computer and use it in GitHub Desktop.

Select an option

Save maasg/6479884 to your computer and use it in GitHub Desktop.
Running Lenght Encoding problem: Given a string with repeated characters, return a string with the repeating characters and their frequency: e.g. aaabbc = a3b2c1
object RunningLengthEncoding {
def encode(s:String):String = {
def encSeq(s:String):String = s.head.toString+s.length
if (s.isEmpty()) "" else {
val (same,rest) = s.span(_==s(0));
encSeq(same) ++ encode(rest)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment