Long But Easy C++ Solution :


#1

string add_indentation(int count){
string str="";
while(count–){
str+=’\t’;
}
return str;
}

vector Solution::prettyJSON(string A) {

string str="";
int count=0;
stack<int> s;
vector<string> v;
s.push(0);
for(int i=0;i<A.length();){


    if(A[i]=='{' || A[i]=='['){
        
        if(!str.empty()){
            v.push_back(str);
            str="";
        }
        
        int val=s.top();
        str+=add_indentation(val);
        str+=A[i];
        v.push_back(str);
        count++;
        s.push(count);
        str="";
        i++;
    }
    
    else if(A[i]=='}' || A[i]==']'){
        
        if(!str.empty()){
            v.push_back(str);
            str="";
        }
        
        s.pop();
        count--;
        int val=s.top();
        str+=add_indentation(val);
        str+=A[i];
        
        if(A[i+1]==','){
            str+=A[i+1];
            i++;
        }
        v.push_back(str);
        str="";
        i++;
    }
    
    else if(A[i]==','){
        
        str+=A[i];
        v.push_back(str);
        str="";
        i++;
    }
    
    else{
        int val=s.top();
        str+=add_indentation(val);
        while(A[i]!=',' && A[i]!='[' && A[i]!=']' && A[i]!='{' && A[i]!='}'){
            str+=A[i];
            i++;
        }
    }
}

return v;
}