count and say
https://leetcode.com/problems/count-and-say/
class Solution {
public:
string countAndSay(int n) {
if(n == 1)
{
return "1";
}
else if(n == 2)
{
return "11";
}
string oldstr = "11";
string newstr;
for(int i = 2; i<n; i++)
{
newstr.clear();
//cout<<newstr<<endl;
int count = 1;
for(int j = 0; j<oldstr.length(); j++)
{
if(j == oldstr.length()-1 || oldstr[j+1] != oldstr[j])
{
newstr = newstr + to_string(count);
newstr.push_back(oldstr[j]);
//cout<<count<<endl;
//cout<<newstr<<endl;
count = 1;
}
else
{
count++;
}
}
oldstr = newstr;
}
return newstr;
}
};