-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString Compression.cpp
More file actions
39 lines (34 loc) · 1.32 KB
/
String Compression.cpp
File metadata and controls
39 lines (34 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Problem: Perform in-place string compression.
// Given an array of characters, compress it by replacing sequences of repeating
// characters with the character followed by the count of repetitions.
// Example: ['a','a','b','b','c','c','c'] → ['a','2','b','2','c','3']
// Approach:
// 1. Iterate through the array, count consecutive occurrences of each character.
// 2. Place the character at the current answer index.
// 3. If the count > 1, convert count to string and place each digit separately.
// 4. Continue until all characters are processed. Return the new length.
// Time Complexity: O(n), Space Complexity: O(1) (in-place).
class Solution {
public:
int compress(vector<char>& chars) {
int ans = 0;
for (int i = 0; i < chars.size();) {
const char ch = chars[i];
int count = 0;
// Count consecutive occurrences of current char
while (i < chars.size() && chars[i] == ch) {
++count;
++i;
}
// Place the character
chars[ans++] = ch;
// Place the count if > 1
if (count > 1) {
for (const char c : to_string(count)) {
chars[ans++] = c;
}
}
}
return ans; // length of compressed string
}
};