Run Length Encoding


Run Length Encoding is a method of data compression which involves reducing the length of the encoded data by replacing repeated elements in the data sequence with a shorter representation in order to save space.

To give you an example, quoted directly from the January 2015 issue of OpenSource as a coding problem,

“You are given an input string and asked to write a code snippet to return the run length encoded representation of that string. For example, if you are given the string aaaccefhhh, your function should return the string a3c2e1f1h3.”

Notice that the numbers after each alphabet in the output string denote the number of repeated alphabets in the input string sequence. This is Run Length Encoding.

I decided to write this program after a friend challenged me to see if I had the testicular fortitude at coding.

I think it’s fair to say I proved my point. Here’s the program, and it’s free to use for whatever purpose you might have in mind, all I ask for is credit to the original author if the situation permits it:

 import java.io.*;
 import java.util.*;
 
 public class RunLengthEncoding 
 {
     public static void main(String args[]) throws Exception
     {
      String str = new String(); // String variable to hold the value of the string to be encoded.
      String rlestr = new String(); // String variable to hold the final RLE encoded string.
      Integer rlecount = 1, totalcount = 1; // The integers holding the RLE values and the value of the currently under analysis string variable.
      String ch = new String(); // Holds the value of the character in the run.
 
      BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
      System.out.println("Enter the String to be converted to RLE Format:\n");
      str = br.readLine();
      for (int i = 0; i < str.length() - 1; i++) 
      {
          ch = Character.toString(str.charAt(i));
          if( str.charAt(i) == str.charAt(i+1) ) // Checks whether two consecutive letters are equivalent, hereby creating a run.
          {
              rlecount++;
              totalcount++;
           }
           if ( !Character.toString(str.charAt(i+1)).equalsIgnoreCase(ch) ) // Checks when the end of the run occurs and appends to the output string.
           {
               rlestr = rlestr + ch;
               rlestr = rlestr + rlecount.toString();
               rlecount = 1;
               totalcount++;
           }
           if ( i == str.length() - 2 ) // When the end of the input string approaches, it performs the final append operation and breaks out of the loop.
           {
               ch = Character.toString(str.charAt(i+1));
               rlestr = rlestr + ch;
               rlestr = rlestr + rlecount.toString();
               break;
           }
       }
 System.out.println("The RLE encoded String is:");
 System.out.println(rlestr);
 }
}