输出:“a2b1c5a3”
样例 2:
输入:str = “aabbcc”
输出:“aabbcc”
class Solution {
public:
/**
* @param originalString: a string
* @return: a compressed string
*/
string compress(string &originalString) {
// write your code here
int len1=originalString.size();
string result;
int sum=1;
char temp=originalString[0];
for (int i = 1; i < len1; i++) {
/* code */
if(originalString[i]==temp){sum++;}
else
{
result=result+temp+to_string(sum);
temp=originalString[i];
sum=1;
}
}
result=result+temp+to_string(sum);
if(len1>result.size()) return result;
else return originalString;
}
};