patterncsharpModerate
String compression by using repeated characters count
Viewed 0 times
repeatedcompressionusingcharacterscountstring
Problem
My task was to perform a basic string compression by replacing consecutive repeated characters by one instance of the character and integer denoting the number of repetitions.
For example the string "aaaaabbcccdeee" should be reduced to "a5b2c3de3".
I have tried working in C# using a simple logic. Please see the below code. Also I have added the condition to not to add the count 1 in the compressed string if there is only single letter occurrence. E.g. if letter "d" is occurred only once in a string "aaabbcccdee" then function will give a compressed string as "a3b2c3de2" and not the "a3b2c3d1e2". To modify this requirement we can change the condition for this single count.
Please let me know about the comparative time and space complexity and other efficient way in C#.
```
class StringCompression
{
static void Main(string[] args)
{
StringCompression sc = new StringCompression();
sc.CompressionMethod("aaaaabbbccdeeeee");
sc.CompressionMethod("aaabbccdddee");
sc.CompressionMethod("a");
}
public void CompressionMethod(string originalString)
{
List OriginalList = new List();
List CompressedList = new List();
OriginalList.AddRange(originalString);
// Convert to Character Array
char[] charArray = OriginalList.ToArray();
int i = 0;
char character;
int len = charArray.Length;
while (i < len)
{
int n = 0;
character = (charArray[i]);
while (i < charArray.Length && charArray[i] == character)
{
n = n + 1;
i++;
}
// add characters to the new list
CompressedList.Add(character.ToString());
// add character counts to the new list
if (n == 1)
{
// Do nothing
}
else
{
CompressedList.Add(n.ToString());
}
}
For example the string "aaaaabbcccdeee" should be reduced to "a5b2c3de3".
I have tried working in C# using a simple logic. Please see the below code. Also I have added the condition to not to add the count 1 in the compressed string if there is only single letter occurrence. E.g. if letter "d" is occurred only once in a string "aaabbcccdee" then function will give a compressed string as "a3b2c3de2" and not the "a3b2c3d1e2". To modify this requirement we can change the condition for this single count.
Please let me know about the comparative time and space complexity and other efficient way in C#.
```
class StringCompression
{
static void Main(string[] args)
{
StringCompression sc = new StringCompression();
sc.CompressionMethod("aaaaabbbccdeeeee");
sc.CompressionMethod("aaabbccdddee");
sc.CompressionMethod("a");
}
public void CompressionMethod(string originalString)
{
List OriginalList = new List();
List CompressedList = new List();
OriginalList.AddRange(originalString);
// Convert to Character Array
char[] charArray = OriginalList.ToArray();
int i = 0;
char character;
int len = charArray.Length;
while (i < len)
{
int n = 0;
character = (charArray[i]);
while (i < charArray.Length && charArray[i] == character)
{
n = n + 1;
i++;
}
// add characters to the new list
CompressedList.Add(character.ToString());
// add character counts to the new list
if (n == 1)
{
// Do nothing
}
else
{
CompressedList.Add(n.ToString());
}
}
Solution
String as an Array
You got your string parameter:
Then you turned it into a list:
Then you turned it into an array:
And finally used it as:
Instead of all that, you could have just done this:
Class Design
Instantiating a StringCompression class every time you wanna compress a string can be avoided by just declaring the function CompressionMethod as static since the class has no members anyway. So in Main, you only have to do:
Or better: make it an extension method.
String Builder
Instead of a list of strings in:
You could instead use a String Builder to create the resulting string.
You got your string parameter:
string originalStringThen you turned it into a list:
OriginalList.AddRange(originalString);Then you turned it into an array:
charArray = OriginalList.ToArray();And finally used it as:
character = (charArray[i]);Instead of all that, you could have just done this:
character = (originalString[i]);Class Design
Instantiating a StringCompression class every time you wanna compress a string can be avoided by just declaring the function CompressionMethod as static since the class has no members anyway. So in Main, you only have to do:
StringCompression.CompressionMethod("String here");Or better: make it an extension method.
String Builder
Instead of a list of strings in:
CompressedList = new List();You could instead use a String Builder to create the resulting string.
Code Snippets
string originalStringOriginalList.AddRange(originalString);charArray = OriginalList.ToArray();character = (charArray[i]);character = (originalString[i]);Context
StackExchange Code Review Q#109076, answer score: 11
Revisions (0)
No revisions yet.