Skip to content

Instantly share code, notes, and snippets.

@dmnugent80
Created April 26, 2015 20:37
Show Gist options
  • Save dmnugent80/a18e43675830453e046b to your computer and use it in GitHub Desktop.
Save dmnugent80/a18e43675830453e046b to your computer and use it in GitHub Desktop.
Condense String to character and count
public class CondenseString
{
public static void main(String[] args)
{
StringBuilder sb = new StringBuilder();
String test = "aaaabbbcccccccdeee";
char ch = test.charAt(0);
int count = 1;
for (int i = 1; i <= test.length(); i++){
if (i == test.length() || ch != test.charAt(i)){
sb.append(ch);
sb.append(count);
if (i < test.length())
ch = test.charAt(i);
count = 1;
}
else{
count++;
}
}
System.out.print(sb);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment