title: I want the string pattern aabbcc to be displayed as 2a2b2c
body: <p>I have somehow got the output with the help of some browsing. But I couldn't understand the logic behind the code. Is there any simple way to achieve this?</p>
  
  <pre><code>public class LetterCount {
    public static void main(String[] args)
    {
      String str = "aabbcccddd";
      int[] counts = new int[(int) Character.MAX_VALUE];
      // If you are certain you will only have ASCII characters, I would use `new int[256]` instead
  
      for (int i = 0; i &lt; str.length(); i++) {
          char charAt = str.charAt(i);
          counts[(int) charAt]++;
      }
  
      for (int i = 0; i &lt; counts.length; i++) {
          if (counts[i] &gt; 0)
              //System.out.println("Number of " + (char) i + ": " + counts[i]);
              System.out.print(""+ counts[i] + (char) i + "");
      }
    }
  }    
  </code></pre>