Oracle | Phone Screen | Run-length Encoding
8505
Nov 07, 2017
Jul 31, 2019
Below question asked during oracle Phone screening, though to share.
Given a string, output the run length encoding of that string so that repeat letters are compressed to the character and count.
Example:
abcccddeeeeea => abc3d2e5a
string CompressString(string str)
{
if (str.Length < 2)
{
return str;
}
StringBuilder output = new StringBuilder();
int count =1;
for (int i=0 ; i < str.Length; i++ )
{
if (i+1 < str.Length && str[i] == str[i+1] )
{
count++;
i++;
}
if (count > 1)
{
output.Append(count);
count =1;
}
i++;
}
return output.ToString();
}
}Comments (20)


