/* ---------------------------------------------------------
Compress: turn "aaabbcccc" into "a3b2c4"
This is run-length encoding (RLE)
--------------------------------------------------------- */
function compress_string(string $s): string {
if ($s === "") {
return ""; // Edge case: empty string
}
$out = ""; // Result string
$count = 1; // Count of repeated characters
$len = strlen($s);
// Start from index 1 and compare with previous character
for ($i = 1; $i <= $len; $i++) {
// If still repeating the same character, increase count
if ($i < $len && $s[$i] === $s[$i - 1]) {
$count++;
} else {
// Character changed OR reached end of string
$out .= $s[$i - 1]; // Add the character
$out .= $count; // Add how many times it repeated
$count = 1; // Reset counter
}
}
return $out;
}
/* ---------------------------------------------------------
Decompress: turn "a3b2c4" into "aaabbcccc"
Reads a letter, then reads digits, expands them.
--------------------------------------------------------- */
function decompress_string(string $s): string {
$out = ""; // Result string
$currentChar = null; // The character being processed
$number = ""; // Digits representing the count
$len = strlen($s);
for ($i = 0; $i < $len; $i++) {
$c = $s[$i];
if (ctype_alpha($c)) { // Found a letter
// If we already have a previous letter + number, expand it
if ($currentChar !== null && $number !== "") {
$count = intval($number);
$out .= str_repeat($currentChar, $count);
}
$currentChar = $c; // Start new character
$number = ""; // Reset number buffer
}
else if (ctype_digit($c)) { // Found a digit
$number .= $c; // Build multi-digit number
}
}
// Handle the last character + number pair
if ($currentChar !== null && $number !== "") {
$count = intval($number);
$out .= str_repeat($currentChar, $count);
}
return $out;
}
function main() {
$s = "wwwwwwwwwwwwbwwwwwwwwwwbbbwwwwwwccccwwwwwwwwwwww";
$c = compress_string($s);
$d = decompress_string($c);
echo "Original: $s\n";
echo "Compressed: $c\n";
echo "Decompressed: $d\n";
}
main();
/*
run:
Original: wwwwwwwwwwwwbwwwwwwwwwwbbbwwwwwwccccwwwwwwwwwwww
Compressed: w12b1w10b3w6c4w12
Decompressed: wwwwwwwwwwwwbwwwwwwwwwwbbbwwwwwwccccwwwwwwwwwwww
*/