Last active
December 22, 2015 13:38
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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