Skip to content

Instantly share code, notes, and snippets.

@d3ep4k
Created January 6, 2015 11:26
Show Gist options
  • Save d3ep4k/52e4ce03c4baff04d77d to your computer and use it in GitHub Desktop.
Save d3ep4k/52e4ce03c4baff04d77d to your computer and use it in GitHub Desktop.
String Encoding - Character Count
str = 'aaaabbbccddddd'
out = ''
Stack s = new Stack()
i=0
while(i<str.length()){
//if element matched
if(!s.empty() && s.peek() != str[i] ){
out = out + s.peek() + '' + s.size()
//empty the stack
s = new Stack()
}
//push to stack
s.push(str[i])
i++
}
//for last pattern
if(i == str.length()){
out = out + s.peek() + '' + s.size()
}
println out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment