c++ solution with explaination , 100% faster tc
EXPLAINED AT COMMENTS\n# Code\n\nclass Solution {\npublic:\n    string countOfAtoms(string f) {\n        map<string,int> m;\n        f+=\')\';\n        int n=f.
EXPLAINED AT COMMENTS\n# Code\n```\nclass Solution {\npublic:\n    string countOfAtoms(string f) {\n        map<string,int> m;\n        f+=\')\';\n        int n=f.size();\n        stack<pair<string,int>> s;\n        s.push({"(",0});    //opening bracket to start\n\n        for(int i=0;i<n;){\n\n            if(f[i]>=\'A\'&& f[i]<=\'Z\'){  //if its a atom\n                int j=i;\n                string atom="";\n                atom+=f[j++];\n                while(f[j]>=\'a\'&&f[j]<=\'z\')  atom+=f[j++];  //get the full name of that atom\n\n                int freq=0;\n                while(f[j]>=\'0\' && f[j]<=\'9\')   freq=freq*10+f[j++]-\'0\';    //now get the frequency of that atom\n                if(freq==0) freq=1;\n                s.push({atom,freq});    //push it in the stack\n                i=j;\n            }\n            else if(f[i]==\')\'){ //if its a closing bracket\n\n                int j=i+1;\n                int freq=0;\n                while(j<n && f[j]>=\'0\' && f[j]<=\'9\')   \n                    freq=freq*10+f[j++]-\'0\';    //get the multiple of that bracket  \n                if(freq==0) freq=1;\n                //and remove current bracket atoms and put on just outer brackets with freq multiplied by multiple\n                stack<pair<string,int>> temp;\n                while(s.top().first!="("){\n                    auto t=s.top(); s.pop();\n                    temp.push({t.first,t.second*freq});\n                }\n                s.pop();\n                while(temp.size()){\n                    s.push(temp.top()); temp.pop();\n                }\n                i=j;\n            }\n            else if(f[i]==\'(\') { s.push({"(",0});   i++;}\n        }\n        while(s.size()){\n            auto t=s.top(); s.pop();\n           m[t.first]+=t.second;    //put the content of stack in a map at the end to get sorted order\n        }\n        string ans="";\n        for(auto x:m){\n            ans+=x.first+(x.second>1?to_string(x.second):"");   //keep frequency only if its more than 1\n        }  \n        return ans;\n    }\n};\n```
['C++']