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

1 Answer

0 votes
import java.lang.Character;
import java.lang.Integer;
import java.lang.String;
import java.lang.StringBuilder;
import java.util.Arrays;

/** ---------------------------------------------------------
   Compress: turn "aaabbcccc" into "a3b2c4"
   This is run-length encoding (RLE)
   --------------------------------------------------------- */
public class RLE {

    public static String compress(String s) {
        if (s.isEmpty()) return "";   // Edge case: empty string

        StringBuilder out = 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.charAt(i) == s.charAt(i - 1)) {
                count++;
            }
            else {
                // Character changed OR reached end of string
                out.append(s.charAt(i - 1));        // Add the character
                out.append(count);                  // Add how many times it repeated
                count = 1;                          // Reset counter
            }
        }
        return out.toString();
    }

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

        for (char c : s.toCharArray()) {

            if (Character.isLetter(c)) {          // Found a letter
                // If we already have a previous letter + number, expand it
                if (currentChar != '\0' && number.length() > 0) {
                    int count = Integer.parseInt(number.toString());
                    out.append(String.valueOf(currentChar).repeat(count));
                }
                currentChar = c;                  // Start new character
                number.setLength(0);              // Reset number buffer
            }
            else if (Character.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 = Integer.parseInt(number.toString());
            out.append(String.valueOf(currentChar).repeat(count));
        }

        return out.toString();
    }

    public static void main(String[] args) {
        String s = "wwwwwwwwwwwwbwwwwwwwwwwbbbwwwwwwccccwwwwwwwwwwww";

        String c = compress(s);
        String d = decompress(c);

        System.out.println("Original:   " + s);
        System.out.println("Compressed: " + c);
        System.out.println("Decompressed: " + d);
    }
}


/*
run:

Original:   wwwwwwwwwwwwbwwwwwwwwwwbbbwwwwwwccccwwwwwwwwwwww
Compressed: w12b1w10b3w6c4w12
Decompressed: wwwwwwwwwwwwbwwwwwwwwwwbbbwwwwwwccccwwwwwwwwwwww

*/

 



answered 1 day ago by avibootz

Related questions

...