Easy to understand C++ code O(n)


#1
string getString(int n){
string a = "";
while(n>0){
    a += "\t";
    n--;
}
return a;

}
vector Solution::prettyJSON(string A) {
vector ans;
if (A.size()==0)
return ans;

string s= "";
s += A[0];
ans.push_back(s);
int cnt = 1;
s= getString(cnt);
bool n = true;
for(int i=1;i<A.size();i++){
    if (A[i]== '[' || A[i]== '{'|| A[i]== ',' || A[i]==']'|| A[i]== '}'){
        
        if (A[i]== ','){
            s += A[i];
            ans.push_back(s);
            s = getString(cnt);
            n = true;
        }
        else{
            
            if (!n){
                ans.push_back(s);
                n= true;
                }

            if (A[i]== '[' || A[i]== '{'){
                s = getString(cnt);
                s += A[i];
                ans.push_back(s);
                cnt++;
                s = getString(cnt);
                // cout << "s = "<<s;
                n=true;
                }
            else{
                cnt--;
                s = getString(cnt);
                s += A[i];
                // ans.push_back(s);
                // s = getString(cnt);
                n=false;
                } 
            
            }
    }
    else{
        s += A[i];
        n = false;
        }
}
ans.push_back(s);

return ans;
}