language: C++11 (gcc-4.7.2)
date: 345 days 15 hours ago
link:
visibility: public
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
#include <string>
#include <iostream>
#include <sstream>
 
//slower, but handles strings of characters less than 2 billion
std::string condense(const std::string& str){
    std::string ret;
    if (str.length()>0) {
        auto begin=str.begin();
        for(auto i=begin+1; i!=str.end(); ++i) {
            if(*i != *begin) {
                std::stringstream ss;
                ss << distance(begin, i);
                ret.append(ss.str());
                ret.append(1, *begin);
                begin = i;
            }
        }
    }
    return ret;
}
 
int main(){
    std::string in = "aaabbbbcccccdde";
    std::cout << condense(in);  //prints 3a4b5c2de
}