My simple c++ solution with o(n2) time complexity


#1

string Solution::countAndSay(int A) {
string s1=“1”;
if(A==1) return s1;
string prev=“11”;
int l=3;
while(l<=A){
string cur="";
int cnt=1;
for(int i=0;i<prev.size();i++){
if(i==prev.size()-1){
cur+=to_string(cnt)+prev[i]; break;
}
else if(i!=prev.size()-1 && prev[i]==prev[i+1]) {
cnt++;
continue;
}
else{
cur+=to_string(cnt)+prev[i];
cnt=1;
}
}
prev=cur;
l++;
}
return prev;
}