The Indian Engineer

Problem 3163 String Compression III

Posted on 6 mins

String

Problem Statement

Link - Problem 3163

Question

Given a string word, compress it using the following algorithm:

Return the string comp.

Example 1:

Input: word = "abcde"

Output: "1a1b1c1d1e"

Explanation:

Initially, comp = "". Apply the operation 5 times, choosing "a", "b", "c", "d", and "e" as the prefix in each operation.

For each prefix, append "1" followed by the character to comp.

Example 2:

Input: word = "aaaaaaaaaaaaaabb"

Output: "9a5a2b"

Explanation:

Initially, comp = "". Apply the operation 3 times, choosing "aaaaaaaaa", "aaaaa", and "bb" as the prefix in each operation.

  • For prefix "aaaaaaaaa", append "9" followed by "a" to comp.
  • For prefix "aaaaa", append "5" followed by "a" to comp.
  • For prefix "bb", append "2" followed by "b" to comp.

Constraints:

Solution

class Solution {
public:
    string compressedString(string word) {
        string comp = "";
        int size = word.size();
        comp.reserve(size*2);
        int ptr = 0;

        while (ptr < size) {
            int frq = 1;
            char ch = word[ptr];
            ptr++;

            while (ptr < size && word[ptr] == ch && frq < 9) {
                ptr++;
                frq++;
            }

            comp += (frq + '0');
            comp += ch;
        }

        return comp;
    }
};

Complexity Analysis

| Algorithm           | Time Complexity | Space Complexity |
| ------------------- | --------------- | ---------------- |
| Run-length encoding | O(n)            | O(n)             |

Explanation

Intial Thoughts

To solve this problem, we should start by understanding the compression algorithm. It involves removing a prefix of the input string that consists of the same character repeated at most 9 times. We then append the length of this prefix and the character itself to the result string. This process repeats until the input string is empty. Key points to consider are:

  1. Identify the repeated character prefixes in the string.
  2. Determine the length of each prefix, ensuring it doesn’t exceed 9.
  3. Append the prefix length and character to the result string.
  4. Continue the process until the entire string is processed.
  5. Handle edge cases, such as an empty input string or single-character strings.

Intuitive Analysis

Intuitively, we can approach this problem by thinking of it as a process of scanning the input string from left to right, identifying blocks of consecutive identical characters, and representing each block with its length and the character. Important insights include:

  1. Use a pointer to track the current position in the string.
  2. Compare characters to identify blocks of the same character.
  3. Keep a count of the length of each block.
  4. When a block ends, append its length and character to the result string.
  5. Repeat this process until the end of the string is reached, resulting in the compressed string.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Each time, just cut the same character in prefix up to at max 9 times. It’s always better to cut a bigger prefix.


Similar Questions:

Title URL Difficulty
String Compression https://leetcode.com/problems/string-compression Medium
String Compression II https://leetcode.com/problems/string-compression-ii Hard