Question: I am currently implementing Run Length Encoding for text compression and my algorithm does return Strings of the following form:

Let's say we have a string as input

```
"AAAAABBBBCCCCCCCC"

```

then my algorithm returns 

```
"1A2A3A4A5A1B2B3B4B1C2C3C4C5C6C7C8C"

```

Now I want to apply Java String split to solve this, because I want to get the highest number corresponding to character. For our example it would be

```
"5A4B8C"

```

My function can be seen below

```
public String getStrfinal(){

    String result = "";
    int counter = 1;
    StringBuilder sb = new StringBuilder();
    sb.append("");
    for (int i=0;i<str.length()-1;i++) {
        char c = str.charAt(i);
        if (str.charAt(i)==str.charAt(i+1)) {
            counter++;
            sb.append(counter);
            sb.append(c);
        }
        else {
            counter = 1;
            continue;
        }
    }
    result = sb.toString();
    return result;
}

```

Answer: 
Instead of string Buffer you can use a map it will be much easier and clean to do so.

```
public static void main(String[] args)  {   
    String input = "AAAAABBBBCCCCCCCCAAABBBDDCCCC";
    int counter=1;
    for(int i=1; i<input.length(); i++) {
        if(input.charAt(i-1)==input.charAt(i)) {
            counter=counter+1;
        }else if(input.charAt(i-1)!=input.charAt(i)){
            System.out.print(counter+Character.toString(input.charAt(i-1)));
            counter=1;
        }if(i==input.length()-1){
            System.out.print(counter+Character.toString(input.charAt(i)));
        }
    }   
}

```

This will gives 

```
5A4B8C3A3B2D4C

```

**UPDATES**  

I Agree with @WJS if the string contains number the out put becomes messy 
hence if the System.out in above code will be exchange with below i.e.

```
System.out.print(Character.toString(input.charAt(i-1))+"="+counter+" ");

```

then for input like 

```
AAAAABBBBCCCCCCCCAAABBBDD556677CCCCz

```

we get out put as below

```
A=5 B=4 C=8 A=3 B=3 D=2 5=2 6=2 7=2 C=4 z=1

```
This is one of the possible solutions to your question. We can use a LinkedHashMap data structure which is similar to HashMap but it also maintains the order. So, we can traverse the string and store the occurrence of each character as Key-value pair into the map and retrieve easily with its maximum occurrence.

```
public String getStrFinal(String str){
    if(str==null || str.length()==0) return str;
    LinkedHashMap<Character,Integer> map = new LinkedHashMap<>();
    StringBuilder sb=new StringBuilder(); // to store the final string
    for(char ch:str.toCharArray()){
        map.put(ch,map.getOrDefault(ch,0)+1);  // put the count for each character
    }
    for(Map.Entry<Character,Integer> entry:map.entrySet()){  // iterate the map again and append each character's occurence into stringbuilder
        sb.append(entry.getValue());
        sb.append(entry.getKey());
    }

    System.out.println("String = " + sb.toString()); // here you go, we got the final string
    return sb.toString();
}

```