Program to compress a string of characters I was given this small task in an interview recently. I'm usually terrible at coding questions in interviews, because with the time constraint and the lack of google I usually overthink and rush stuff and just end up with a mess of a program. 

The task was to write a program that would compress the length of string, and compare the length of the compressed string to the input string and return whichever one is smaller. Example would be: input string "aaabcccc" would be compressed to "a3b1c4". 

I essentially split the string into a charArray and then counted each occurrence of a character and stored them into a hashmap, and looped through the map to build the new string. Just from a learning perspective, was there a better way to do this? I was given ~10 minutes to write it as well, it more to assess how I would solve the problem as opposed to the code itself. But regardless, I'd like a review of it: 

import java.util.HashMap;
import java.util.Map.Entry;

public class Compressor {

    public static void main(String args []) {

        String randomString = "aaabccccc";

        HashMap&lt;Character, Integer&gt; map = countCharacters(randomString);    
        String compressedString = createCompressedString(map);

        if(randomString.toCharArray().length &lt; compressedString.toCharArray().length) {
            System.out.println(randomString);
        }
        else {
            System.out.println(compressedString);
        }       
    }

    /**
     * Create hasmap to store character and count of occurrence
     * @param s
     * @return
     */
    private static HashMap&lt;Character, Integer&gt; countCharacters(String s) {

        HashMap&lt;Character, Integer&gt; characterCount = new HashMap&lt;Character, Integer&gt;();
        char[] characterArray = s.toCharArray();

        for(Character c : characterArray) {
            int newCount;
            Integer count = characterCount.get(c);
            if(count == null) {
                newCount = 1;
            }
            else {
                newCount = count + 1;
            }           
            characterCount.put(c, newCount);
        }

        return characterCount;      
    }

    /**
     * Convert hashmap into a string
     * @param map
     * @return
     */
    private static String createCompressedString(HashMap&lt;Character, Integer&gt; map) {

        String newString = "";
        for (Entry&lt;Character, Integer&gt; entry : map.entrySet()) {
            Character key = entry.getKey();
            Integer value = entry.getValue();

            newString += "" + key + "" + value;
        }

        return newString
    }
}
For better performance you should be able to eliminate the Hashmap and use a StringBuilder to count and build the compressed string in one operation:

public static String Compress(String input)
{
    StringBuilder sb = new StringBuilder();
    int i = 0;
    int limit = input.length() - 1;
    char next = '\0';
    while (i &lt; limit)
    {
        int count = 1;
        next = input.charAt(i++);
        while (i &lt;= limit &amp;&amp; next == input.charAt(i))
        {
            count++;
            i++;
        }
        sb.append(next);
        sb.append(count);
    }
    if(i == limit)
    {
        sb.append(input.charAt(i));
        sb.append(1);
    }
    return sb.toString();
}


Caveat:  This code won't work with the full unicode set