|
| 1 | +--- |
| 2 | +id: string-compression |
| 3 | +title: String Compression |
| 4 | +sidebar_label: 0443 - String Compression |
| 5 | +tags: |
| 6 | + - Two Pointers |
| 7 | + - Array |
| 8 | + - Sliding Window |
| 9 | +description: "This is a solution to the String Compression problem on LeetCode." |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +Given an array of characters `chars`, compress it using the following algorithm: |
| 15 | + |
| 16 | +Begin with an empty string `s`. For each group of **consecutive repeating characters** in `chars`: |
| 17 | + |
| 18 | +If the group's length is `1`, append the character to `s`. |
| 19 | +Otherwise, append the character followed by the group's length. |
| 20 | +The compressed string s **should not be returned separately**, but instead, be stored **in the input character array** `chars`. Note that group lengths that are `10` or longer will be split into multiple characters in `chars`. |
| 21 | + |
| 22 | +After you are done **modifying the input array**, return the new length of the array. |
| 23 | + |
| 24 | +You must write an algorithm that uses only constant extra space. |
| 25 | + |
| 26 | +### Examples |
| 27 | + |
| 28 | +**Example 1:** |
| 29 | + |
| 30 | +``` |
| 31 | +Input: chars = ["a","a","b","b","c","c","c"] |
| 32 | +Output: Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"] |
| 33 | +Explanation: The groups are "aa", "bb", and "ccc". This compresses to "a2b2c3". |
| 34 | +``` |
| 35 | + |
| 36 | +**Example 2:** |
| 37 | + |
| 38 | +``` |
| 39 | +Input: chars = ["a"] |
| 40 | +Output: Return 1, and the first character of the input array should be: ["a"] |
| 41 | +Explanation: The only group is "a", which remains uncompressed since it's a single character. |
| 42 | +``` |
| 43 | + |
| 44 | +### Constraints |
| 45 | + |
| 46 | +- `1 <= chars.length <= 2000` |
| 47 | +- `chars[i]` is a lowercase English letter, uppercase English letter, digit, or symbol. |
| 48 | + |
| 49 | +## Solution for String Compression |
| 50 | + |
| 51 | +### Approach |
| 52 | + |
| 53 | +#### Intuition |
| 54 | + |
| 55 | +First, we make the following observation. Consider a group `t` of consecutive repeating characters. The length of compressed `t` is less than or equal to the length of `t`. For example, `d` tranforms into `d`, `cc` into `c2`, `aaaa` into `a4`, `bbbbbbbbbbbb` into `b12`. |
| 56 | + |
| 57 | +This observation allows processing groups in the array `chars` from left to right. |
| 58 | + |
| 59 | +#### Algorithm |
| 60 | + |
| 61 | +1. Declare the variables i – the first index of the current group, and res – the length of the answer (of the compressed string). Initialize i = 0, res = 0. |
| 62 | + |
| 63 | +2. While i is less than the length of chars: |
| 64 | + - Find the length of the current group of consecutive repeating characters groupLength. |
| 65 | + - Add chars[i] to the answer (chars[res++] = chars[i]). |
| 66 | + - If groupLength > 1, add the string representation of groupLength to the answer and increase res accordingly. |
| 67 | + - Increase i by groupLength and proceed to the next group. |
| 68 | + |
| 69 | +3. Return res. |
| 70 | + |
| 71 | +## Code in Different Languages |
| 72 | + |
| 73 | +<Tabs> |
| 74 | +<TabItem value="cpp" label="C++"> |
| 75 | + <SolutionAuthor name="@Shreyash3087"/> |
| 76 | + |
| 77 | +```cpp |
| 78 | +class Solution { |
| 79 | +public: |
| 80 | + int compress(vector<char>& chars) { |
| 81 | + int i = 0, res = 0; |
| 82 | + while (i < chars.size()) { |
| 83 | + int groupLength = 1; |
| 84 | + while (i + groupLength < chars.size() && chars[i + groupLength] == chars[i]) { |
| 85 | + groupLength++; |
| 86 | + } |
| 87 | + chars[res++] = chars[i]; |
| 88 | + if (groupLength > 1) { |
| 89 | + for (char c : to_string(groupLength)) { |
| 90 | + chars[res++] = c; |
| 91 | + } |
| 92 | + } |
| 93 | + i += groupLength; |
| 94 | + } |
| 95 | + return res; |
| 96 | + } |
| 97 | +}; |
| 98 | + |
| 99 | + |
| 100 | +``` |
| 101 | +</TabItem> |
| 102 | +<TabItem value="java" label="Java"> |
| 103 | + <SolutionAuthor name="@Shreyash3087"/> |
| 104 | +
|
| 105 | +```java |
| 106 | +class Solution { |
| 107 | + public int compress(char[] chars) { |
| 108 | + int i = 0, res = 0; |
| 109 | + while (i < chars.length) { |
| 110 | + int groupLength = 1; |
| 111 | + while (i + groupLength < chars.length && chars[i + groupLength] == chars[i]) { |
| 112 | + groupLength++; |
| 113 | + } |
| 114 | + chars[res++] = chars[i]; |
| 115 | + if (groupLength > 1) { |
| 116 | + for (char c : Integer.toString(groupLength).toCharArray()) { |
| 117 | + chars[res++] = c; |
| 118 | + } |
| 119 | + } |
| 120 | + i += groupLength; |
| 121 | + } |
| 122 | + return res; |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +</TabItem> |
| 128 | +<TabItem value="python" label="Python"> |
| 129 | + <SolutionAuthor name="@Shreyash3087"/> |
| 130 | + |
| 131 | +```python |
| 132 | +class Solution: |
| 133 | + def compress(self, chars: List[str]) -> int: |
| 134 | + i = 0 |
| 135 | + res = 0 |
| 136 | + while i < len(chars): |
| 137 | + group_length = 1 |
| 138 | + while (i + group_length < len(chars) |
| 139 | + and chars[i + group_length] == chars[i]): |
| 140 | + group_length += 1 |
| 141 | + chars[res] = chars[i] |
| 142 | + res += 1 |
| 143 | + if group_length > 1: |
| 144 | + str_repr = str(group_length) |
| 145 | + chars[res:res+len(str_repr)] = list(str_repr) |
| 146 | + res += len(str_repr) |
| 147 | + i += group_length |
| 148 | + return res |
| 149 | +``` |
| 150 | +</TabItem> |
| 151 | +</Tabs> |
| 152 | + |
| 153 | +## Complexity Analysis |
| 154 | + |
| 155 | +### Time Complexity: $O(N)$ |
| 156 | + |
| 157 | +> **Reason**: All cells are initially white. We will repaint each white cell blue, and we may repaint some blue cells green. Thus each cell will be repainted at most twice. Since there are n cells, the total number of repaintings is O(n). |
| 158 | + |
| 159 | +### Space Complexity: $O(1)$ |
| 160 | + |
| 161 | +> **Reason**: We store only a few integer variables and the string representation of groupLength which takes up O(1) space. |
| 162 | + |
| 163 | +## References |
| 164 | + |
| 165 | +- **LeetCode Problem**: [String Compression](https://leetcode.com/problems/string-compression/description/) |
| 166 | + |
| 167 | +- **Solution Link**: [String Compression](https://leetcode.com/problems/string-compression/solutions/) |
0 commit comments