APS HSPC
Practice Problems Home

String Compression

Implement a string compression algorithm that takes a string and returns a compressed version of it. The compressed version should be the original string with any repeated characters replaced with the character in parentheses and the number of times it is repeated. Non-repeated characters should not be compressed.

Input

The first line of the input is the number of string to compress, n. The next n lines consist of one string to compress. The strings only contain lowercase letters, with no punctuation or special characters.

Output

The output should be n lines with each compressed string on it’s own line.

Sample Input

2
aaabbbcccd
abcde

Sample Output

(a)3(b)3(c)3d
abcde

Samples

Python

# returns the compressed string
def compress(input):
    #### WRITE CODE HERE ####
    pass
    
num_cases = int(input())
for _ in range(num_cases):
    print(compress(input()))

Java

import java.util.Scanner;

class Sample {
    public static String compress(String input) {
        return ""; 
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        int num_cases = Integer.parseInt(scan.nextLine());
        for (int t = 0; t < num_cases; t++) {
            System.out.println(compress(scan.nextLine()));
        }
    }

}

Real Test Cases

input.txt

output.txt

Solutions

Python Solution
# returns the compressed string
def compress(input):
    output = ""
    current_char = list(input)[0]
    current_count = 1

    for character in list(input)[1:]:
        if character == current_char:
            current_count += 1
            continue

        if current_count != 1:
            output += f"({current_char}){current_count}"
        else:
            output += current_char
        current_count = 1
        current_char = character

    if current_count != 1:
        output += f"({current_char}){current_count}"
    else:
        output += current_char

    return output
    
num_cases = int(input())
for _ in range(num_cases):
    print(compress(input()))
Java Solution
import java.util.Scanner;

class Sample {
    public static String compress(String input) {
        String output = "";
        char current_char = input.charAt(0);
        int current_count = 1;

        for (int i = 1; i < input.length(); i++) {
            char character = input.charAt(i);
            if (character == current_char) {
                current_count++;
                continue;
            }

            if (current_count != 1) {
                output += String.format("(%c)%d", current_char, current_count);
            } else {
                output += current_char;
            }

            current_count = 1;
            current_char = character;
        }

        if (current_count != 1) {
            output += String.format("(%c)%d", current_char, current_count);
        } else {
            output += current_char;
        }

        return output;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        int num_cases = Integer.parseInt(scan.nextLine());
        for (int t = 0; t < num_cases; t++) {
            System.out.println(compress(scan.nextLine()));
        }
    }

}
© Copyright 2024. Inspiration from the UVA HSPC website. Website created by Nate Levin and Brayden Zee.