Run Length Encoding and Decoding
Last Updated :
16 Jun, 2022
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”
Follow the steps below to solve this problem:
- Pick the first character from the source string.
- Append the picked character to the destination string.
- Count the number of subsequent occurrences of the picked character and append the count to the destination string.
- Pick the next character and repeat steps 2, 3 and 4 if the end of the string is NOT reached.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printRLE(string str)
{
int n = str.length();
for ( int i = 0; i < n; i++) {
int count = 1;
while (i < n - 1 && str[i] == str[i + 1]) {
count++;
i++;
}
cout << str[i] << count;
}
}
int main()
{
string str = "wwwwaaadexxxxxxywww" ;
printRLE(str);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_RLEN 50
char * encode( char * src)
{
int rLen;
char count[MAX_RLEN];
int len = strlen (src);
char * dest = ( char *) malloc ( sizeof ( char ) * (len * 2 + 1));
int i, j = 0, k;
for (i = 0; i < len; i++) {
dest[j++] = src[i];
rLen = 1;
while (i + 1 < len && src[i] == src[i + 1]) {
rLen++;
i++;
}
sprintf (count, "%d" , rLen);
for (k = 0; *(count + k); k++, j++) {
dest[j] = count[k];
}
}
dest[j] = '\0' ;
return dest;
}
int main()
{
char str[] = "wwwwaaadexxxxxxywww" ;
char * res = encode(str);
printf ( "%s" , res);
getchar ();
}
|
Java
import java.io.*;
public class GFG {
public static void encoding(String str)
{
int n = str.length();
for ( int i = 0 ; i < n; i++) {
int count = 1 ;
while (i < n - 1
&& str.charAt(i) == str.charAt(i + 1 )) {
count++;
i++;
}
System.out.print(str.charAt(i) + "" + count);
}
}
public static void main(String[] args)
{
String str = "wwwwaaadexxxxxxywww" ;
encoding(str);
}
}
|
Python3
def printRLE(st):
n = len (st)
i = 0
while i < n - 1 :
count = 1
while (i < n - 1 and
st[i] = = st[i + 1 ]):
count + = 1
i + = 1
i + = 1
print (st[i - 1 ] +
str (count),
end = "")
if __name__ = = "__main__" :
st = "wwwwaaadexxxxxxywww"
printRLE(st)
|
C#
using System;
class GFG
{
public class RunLength_Encoding
{
public static void printRLE(String str)
{
int n = str.Length;
for ( int i = 0; i < n; i++)
{
int count = 1;
while (i < n - 1 && str[i] == str[i + 1])
{
count++;
i++;
}
Console.Write(str[i]);
Console.Write(count);
}
}
public static void Main(String[] args)
{
String str = "wwwwaaadexxxxxxywww" ;
printRLE(str);
}
}
}
|
Javascript
<script>
function printRLE(str)
{
let n = str.length;
for (let i = 0; i < n; i++)
{
let count = 1;
while (i < n - 1 && str[i] == str[i+1])
{
count++;
i++;
}
document.write(str[i]);
document.write(count);
}
}
let str = "wwwwaaadexxxxxxywww" ;
printRLE(str);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Please write comments if you find the above code/algorithm incorrect, or find better ways to solve the same problem.
Share your thoughts in the comments
Please Login to comment...