Run Length Encoding

Given an input string, write a function that returns the Run Length Encoded string for the input string.

For example, if the input string is “wwwwaaadexxxxxx”, then the function should return “w4a3d1e1x6”.



a) Pick the first character from source string.
b) Append the picked character to the destination string.
c) Count the number of subsequent occurrences of the picked character and append the count to destination string.
d) Pick the next character and repeat steps b) c) and d) if end of string is NOT reached.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to implement run length encoding
#include <bits/stdc++.h>
using namespace std;
  
void printRLE(string str)
{
    int n = str.length();
    for (int i = 0; i < n; i++) {
  
        // Count occurrences of current character
        int count = 1;
        while (i < n - 1 && str[i] == str[i + 1]) {
            count++;
            i++;
        }
  
        // Print character and its count
        cout << str[i] << count;
    }
}
  
int main()
{
    string str = "wwwwaaadexxxxxxywww";
    printRLE(str);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement run length encoding
  
public class RunLength_Encoding {
    public static void printRLE(String str)
    {
        int n = str.length();
        for (int i = 0; i < n; i++) {
  
            // Count occurrences of current character
            int count = 1;
            while (i < n - 1 && 
                   str.charAt(i) == str.charAt(i + 1)) {
                count++;
                i++;
            }
  
            // Print character and its count
            System.out.print(str.charAt(i));
            System.out.print(count);
        }
    }
  
    public static void main(String[] args)
    {
        String str = "wwwwaaadexxxxxxywww";
        printRLE(str);
    }
}

chevron_right


Output:

w4a3d1e1x6y1w3

C Implementation

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_RLEN 50
  
/* Returns the Run Length Encoded string for the 
   source string src */
char* encode(char* src)
{
    int rLen;
    char count[MAX_RLEN];
    int len = strlen(src);
  
    /* If all characters in the source string are different, 
    then size of destination string would be twice of input string.
    For example if the src is "abcd", then dest would be "a1b1c1d1"
    For other inputs, size would be less than twice.  */
    char* dest = (char*)malloc(sizeof(char) * (len * 2 + 1));
  
    int i, j = 0, k;
  
    /* traverse the input string one by one */
    for (i = 0; i < len; i++) {
  
        /* Copy the first occurrence of the new character */
        dest[j++] = src[i];
  
        /* Count the number of occurrences of the new character */
        rLen = 1;
        while (i + 1 < len && src[i] == src[i + 1]) {
            rLen++;
            i++;
        }
  
        /* Store rLen in a character array count[] */
        sprintf(count, "%d", rLen);
  
        /* Copy the count[] to destination */
        for (k = 0; *(count + k); k++, j++) {
            dest[j] = count[k];
        }
    }
  
    /*terminate the destination string */
    dest[j] = '\0';
    return dest;
}
  
/*driver program to test above function */
int main()
{
    char str[] = "geeksforgeeks";
    char* res = encode(str);
    printf("%s", res);
    getchar();
}

chevron_right


Output:

g1e2k1s1f1o1r1g1e2k1s1

Time Complexity: O(n)

References:
http://en.wikipedia.org/wiki/Run-length_encoding

Please write comments if you find the above code/algorithm incorrect, or find better ways to solve the same problem.



My Personal Notes arrow_drop_up