Very decent and easy to understand C++ solution in o(n) time complexity

interview-questions
Tags: #<Tag:0x00007f24276f62b8>

#1

vector Solution::prettyJSON(string A) {
string intd="";
string k="";
vector ans(0);
for(int i=0;i<A.size();i++)
{
if(A[i]==’{’ || A[i]==’[’)
{
if(k.size()!=0)
{
ans.push_back(intd+k);
k="";
}
ans.push_back(intd+A[i]);
intd+=" “;
}
else if(A[i]==’}’ || A[i]==’]’)
{
if(k.size()!=0)
{
ans.push_back(intd+k);
k=”";
}
intd.pop_back();
intd.pop_back();
intd.pop_back();
intd.pop_back();
ans.push_back(intd+A[i]);
}
else if(A[i]==’,’)
{
k+=",";
ans.push_back(intd+k);
k="";
}
else
{
k+=A[i];

   }
}
return ans;

}