C++ Solution with O(n) Time complexity


#1

vector Solution::prettyJSON(string A)
{
int n = A.length();
int i = 0;
vector ans;
string temp = “”;
int space = 0;
for(i=0;i<n; i++)
{
if(A[i]==’{’ || A[i]==’[’)
{
if(temp=="")
{
int t = space;
temp = A[i];
while(t>0)
{
temp = “\t” + temp;
t–;
}
ans.push_back(temp);
temp = “”;
space++;
}
else
{
int t = space;
while(t>0)
{
temp = “\t” + temp;
t–;
}
ans.push_back(temp);
temp = “”;
//now do as normal { or [

            t = space;
            temp = A[i];
            while(t>0)
            {
                temp = "\t" + temp;
                t--;
            }
            ans.push_back(temp);
            temp = "";
            space++;
            ///
        }
        
    }
    else if(A[i]=='}' || A[i]==']')
    {
        if(temp == "")
        {
            
            if(i<n && A[i+1] == ',')
            {
                temp = A[i];
                temp += A[i+1];
                i++;
                space--;
                int t = space;
                while(t>0)
                {
                    temp = "\t" + temp;
                    t--;
                }
                    
                ans.push_back(temp);
                temp = "";
                
                continue;
            }
            
            space--;
            int t = space;
            temp = A[i];
            while(t>0)
            {
                temp = "\t" + temp;
                t--;
            }
            ans.push_back(temp);
            temp = "";
        }
        else
        {
            int t = space;
            while(t>0)
            {
                temp = "\t" + temp;
                t--;
            }
            ans.push_back(temp);
            temp = "";
            space--;
            
            //now do as normal } or ]
            
            
            if(i<n && A[i+1] == ',')
            {
                temp = A[i];
                temp += A[i+1];
                i++;
                
                int t = space;
                while(t>0)
                {
                    temp = "\t" + temp;
                    t--;
                }
                    
                ans.push_back(temp);
                temp = "";
                
                continue;
            }
            
            
            t = space;
            temp = A[i];
            while(t>0)
            {
                temp = "\t" + temp;
                t--;
            }
            ans.push_back(temp);
            temp = "";
            //
        }
    }
    else
    {
        if(A[i] == ',')
        {
            temp = temp + A[i];
            int t = space;
            while(t>0)
            {
                temp = "\t" + temp;
                t--;
            }
            ans.push_back(temp);
            temp = "";
            continue;
        }
        else if(A[i] == ':')
        {
            if(A[i+1] == '{' || A[i+1] == '[')//then end the string
            {
                temp = temp + A[i];
                int t = space;
                while(t>0)
                {
                    temp = "\t" + temp;
                    t--;
                }
                ans.push_back(temp);
                temp = "";
                continue;
            }
        }
        temp = temp + A[i];
    }
}

return ans;

}