Skip to content

Instantly share code, notes, and snippets.

@ferromir
Created January 20, 2016 22:37
Show Gist options
  • Save ferromir/996eec1029e615714bfe to your computer and use it in GitHub Desktop.
Save ferromir/996eec1029e615714bfe to your computer and use it in GitHub Desktop.
Interview with Christope Verbinnen from Lookout
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