Files
2026-05-08 20:00:09 +01:00

42 lines
1.1 KiB
C++

class Solution {
public:
int compress(vector<char>& chars) {
int count = 0;
vector<char> compressed = {};
char temp = chars.at(0);
for (int i = 0; i < chars.size(); i++)
{
char current = chars.at(i);
if (temp == current) // If temp and current == same then increase count
{
count++;
}
else
{
compressed.push_back(temp);
if (1 < count) {
string s = to_string(count);
for (char c : s) {
compressed.push_back(c);
}
}
temp = current;
count = 1;
}
}
compressed.push_back(temp);
if (1 < count) {
string s = to_string(count);
for (char c : s) {
compressed.push_back(c);
}
}
chars = compressed;
return (compressed.size()); // Get the size of the letters found + the amount found and return it
}
};