How to compress and decompress repeated string characters by storing the repeated length (RLE) in C#

1 Answer

0 votes
using System;
using System.Text;

/* ---------------------------------------------------------
   Compress: turn "aaabbcccc" into "a3b2c4"
   This is run-length encoding (RLE)
   --------------------------------------------------------- */
class RLE
{
    public static string Compress(string s)
    {
        if (string.IsNullOrEmpty(s))
            return "";   // Edge case: empty string

        StringBuilder outStr = new StringBuilder();  // Result string
        int count = 1;                               // Count of repeated characters

        // Start from index 1 and compare with previous character
        for (int i = 1; i <= s.Length; i++) {
            // If still repeating the same character, increase count
            if (i < s.Length && s[i] == s[i - 1]) {
                count++;
            }
            else {
                // Character changed OR reached end of string
                outStr.Append(s[i - 1]);   // Add the character
                outStr.Append(count);      // Add how many times it repeated
                count = 1;                 // Reset counter
            }
        }

        return outStr.ToString();
    }

    /* ---------------------------------------------------------
       Decompress: turn "a3b2c4" into "aaabbcccc"
       Reads a letter, then reads digits, expands them.
       --------------------------------------------------------- */
    public static string Decompress(string s)
    {
        StringBuilder outStr = new StringBuilder();  // Result string
        char currentChar = '\0';                     // The character being processed
        StringBuilder number = new StringBuilder();  // Digits representing the count

        foreach (char c in s) {
            if (char.IsLetter(c)) {  // Found a letter
                // If we already have a previous letter + number, expand it
                if (currentChar != '\0' && number.Length > 0) {
                    int count = int.Parse(number.ToString());
                    outStr.Append(new string(currentChar, count));
                }

                currentChar = c;    // Start new character
                number.Clear();     // Reset number buffer
            }
            else if (char.IsDigit(c)) {  // Found a digit
                number.Append(c);       // Build multi-digit number
            }
        }

        // Handle the last character + number pair
        if (currentChar != '\0' && number.Length > 0) {
            int count = int.Parse(number.ToString());
            outStr.Append(new string(currentChar, count));
        }

        return outStr.ToString();
    }

    static void Main()
    {
        string s = "wwwwwwwwwwwwbwwwwwwwwwwbbbwwwwwwccccwwwwwwwwwwww";

        string c = Compress(s);
        string d = Decompress(c);

        Console.WriteLine("Original:   " + s);
        Console.WriteLine("Compressed: " + c);
        Console.WriteLine("Decompressed: " + d);
    }
}



/*
run:

Original:   wwwwwwwwwwwwbwwwwwwwwwwbbbwwwwwwccccwwwwwwwwwwww
Compressed: w12b1w10b3w6c4w12
Decompressed: wwwwwwwwwwwwbwwwwwwwwwwbbbwwwwwwccccwwwwwwwwwwww

*/

 



answered 1 day ago by avibootz

Related questions

...