dandy
BAN USERpublic static void Compress(String input) {
char[] a = input.toCharArray();
boolean sequence = false;
System.out.println(a);
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int count = 0;
for (int i = 0; i < a.length - 1; i++) {
if (a[i] == a[i + 1]) {
sequence = true;
if (map.containsKey(a[i])) {
map.put(a[i], map.get(a[i]) + 1);
} else {
map.put(a[i], 1);
}
if(i+1==a.length-1){
count = map.get(a[i])+1;
System.out.print(a[i]+""+count);
}
}else
{
count=0;
if(sequence){
count = map.get(a[i])+1;
System.out.print(a[i]+""+count);
}else{
System.out.print(a[i]);
}
if(i+1==a.length-1){
System.out.print(a[i+1]);
}
sequence = false;
}
}
}
O(n)
}
- dandy August 26, 2012