Created
January 6, 2015 11:26
-
-
Save d3ep4k/52e4ce03c4baff04d77d to your computer and use it in GitHub Desktop.
String Encoding - Character Count
This file contains 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
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