Created
January 20, 2016 22:37
-
-
Save ferromir/996eec1029e615714bfe to your computer and use it in GitHub Desktop.
Interview with Christope Verbinnen from Lookout
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
Problem: Implement an algorithim to compress strins such as | |
aaaabbccccccccdddedaaa | |
4a2b8c3d1e1d3a | |
Fernando's solution: pseudocode | |
compress(s string) { | |
if s is empty { | |
return s | |
} | |
ocurrence map[char, int] = // empty... | |
lastChar = null | |
compressed = "" | |
count = 0 | |
foreach c in s { | |
if lastChar == null || c == lastChar { | |
count++ | |
} | |
else { | |
compressed += count + lastChar | |
count = 0 | |
lastChar = c | |
} | |
} | |
return compressed | |
} | |
test cases: | |
compress("") -> "" | |
compress("aaabbbaaa") -> "3a3b3a" | |
compress("abcdef") -> "1a1b1c1d1e1f") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment