C++ easy and short solution


#1

Comment body goes here.
vector Solution::prettyJSON(string A) {
int indentation=0;
string line = “”;
vectorans;
for(int i=0;i<A.size();++i)
{
char x =A[i];
if(x==’ ‘)continue;
if(x==’[’||x==’{’||x==’}’||x==’]’)
{
if(!line.empty())
{
ans.push_back(string(indentation,’\t’).append(line));
line.clear();
}
if(x==’]’||x==’}’)–indentation;
string str = string(indentation,’\t’).append(1,x);
while(i+1<A.size()&&A[i+1]==’,’)
{
str.append(string(1,’,’));
++i;
}
ans.push_back(str);
if(x==’[’||x==’{’)++indentation;
}
else
{
line.append(string(1,x));
if(x==’,’)
{
ans.push_back(string(indentation,’\t’).append(line));
line.clear();
}
}
return ans;
}