Compress String (Character Count)

Compress String (Character Count)

Hard C++ Strings 45 views
Explanation Complexity

Problem Statement

Compress string by showing character and its count. "aaabbc" -> "a3b2c1"
Real Life: Data compression basics.

Input Format

A string S (lowercase or uppercase letters).

Output Format

Compressed string in the form:
character + count

Example

aaabbc
a3b2c1

Constraints

• String length ≥ 1

• Characters are consecutive for counting

• Case-sensitive (a and A are different)

Concept Explanation

String compression replaces repeated characters with the character
followed by the number of times it appears continuously.
This is a basic idea of data compression.

Step-by-Step Explanation

1.Take input string S.

2.Initialize count = 1.

3.Traverse string from index 1 to end.

4.If current character is same as previous:

• Increase count.

5.If current character is different:

• Print previous character and count.

• Reset count = 1.

6.After loop ends, print last character and its count.

Concept Explanation

String compression replaces repeated characters with the character
followed by the number of times it appears continuously.
This is a basic idea of data compression.

Step-by-Step Explanation

1.Take input string S.

2.Initialize count = 1.

3.Traverse string from index 1 to end.

4.If current character is same as previous:

• Increase count.

5.If current character is different:

• Print previous character and count.

• Reset count = 1.

6.After loop ends, print last character and its count.

Input / Output Format

Input Format
A string S (lowercase or uppercase letters).
Output Format
Compressed string in the form:
character + count
Constraints
• String length ≥ 1

• Characters are consecutive for counting

• Case-sensitive (a and A are different)

Examples

Input:
aaabbc
Output:
a3b2c1

Example Solution (Public)

C++
void string_q14_compress_string() {
    string text = "aaabbccccdd";
    string compressed = "";
    
    cout << "Original: " << text << endl;
    
    int i = 0;
    while(i < text.length()) {
        char current = text[i];
        int count = 0;
        
        // Count consecutive same characters
        while(i < text.length() && text[i] == current) {
            count++;
            i++;
        }
        
        compressed += current;
        compressed += to_string(count);
    }
    
    cout << "Compressed: " << compressed << endl;
}

Official Solution Code

void string_q14_compress_string() {
    string text = "aaabbccccdd";
    string compressed = "";
    
    cout << "Original: " << text << endl;
    
    int i = 0;
    while(i < text.length()) {
        char current = text[i];
        int count = 0;
        
        // Count consecutive same characters
        while(i < text.length() && text[i] == current) {
            count++;
            i++;
        }
        
        compressed += current;
        compressed += to_string(count);
    }
    
    cout << "Compressed: " << compressed << endl;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.