Implement a simple compression algorithm where repeated letters in a string are represented by a count and the letter. Example: AAACBBD = 3A1C2B1D
Sigiloso
Here's a simple C++ implementation: string compress(const string& str) { if (str.empty()) return str; string newStr; int count(1); char curr(str[0]); char buff[5]; for (int i = 1; i < str.size(); ++i) { // same char -- increment the counter if (curr == str[i]) ++count; else { // append count and the char itoa(count, buff, 10); newStr += string(buff) + curr; // reset variables curr = str[i]; count = 1; } } // append the last group of char(s) itoa(count, buff, 10); newStr += string(buff) + curr; return newStr; }