O(n^2) Solution C++


#1

O(n^2) solution

string makestring(string s){
int l=s.length();
vector<pair<int, char>> v;
for(int i=0;i<l;i++){
    int j=1;
    while(s[i]==s[i+j] && i+j<l ) j++;
    v.push_back(make_pair(j,s[i]));
    if(j>1){
        i+=j-1;
    }
}
string str="";
for(auto x:v){
    str+=char((x.first)+48);
    str+=x.second;
}
return str;

}

string Solution::countAndSay(int n){
string t=“1”;
while(n>1){
string temp=t;
t=makestring(temp);
n–;
//cout<<t<<endl;
}
return t;
}