O(n) solution , just go step by step


#1
vector<string> Solution::prettyJSON(string A) {
    int n = A.size();
    vector<string > ans;
    string t , newtemp;
    int cntb = 0, x = 0;
    for(int i = 0;i < n;i++){
        char c = A[i];
        if(c == '[' or c == '{'){
            if(!t.empty()){ans.push_back(newtemp + t);t.clear();}//the strings whose end is :
            x = cntb;
            string temp ;
            while(x > 0){
                temp += "\t";
                x--;
            }
            temp += c;
            ans.push_back(temp);
            cntb++;
        }
        else if(c == ']' or c == '}'){
            cntb--;
            if(!t.empty()){ans.push_back(newtemp + t);t.clear();}//the strings whose end is :
            x = cntb;
            string temp ;
            while(x > 0){
                temp += "\t";
                x--;
            }
            temp += c;
            ans.push_back(temp);
        }
        else{
            string temp = "";
            x = cntb;
            while(x > 0){
                temp += "\t";
                x--;
            }
            newtemp = temp;
            t += c;
            if(c==','){
                if(t.size() == 1){
                    string fcuk = ans.back();
                    ans.pop_back();
                    fcuk += t;t.clear();
                    ans.push_back(fcuk);
                }
                else{
                t  = temp + t;
                ans.push_back(t);
                t.clear();
                }
            }
        }
    }
    
    return ans;
}