Created
April 26, 2015 20:37
-
-
Save dmnugent80/a18e43675830453e046b to your computer and use it in GitHub Desktop.
Condense String to character and count
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
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