Just Do what you think intelligently Simple,Clean & easy Stack || c++
So do u remember something like balanced paranthesis problem ?\nif not i recommend first go throught it\'s stack implimentation (https://leetcode.com/problems/v
So do u remember something like balanced paranthesis problem ?\nif not i recommend first go throught it\'s stack implimentation (https://leetcode.com/problems/valid-parentheses/) then come back...\nNow Don\'t you think the structure is similar ?\nLet me help \\\uD83D\uDE01/\n```\nSpace : O(n)\nTime : O(n)\n```\nlet\'s assume a example :\n ```\n   P2( M3(SO4)2 )3   |commas  i have used for better sapration :) \n ```\nAnd here we will traverse string from back u will under stand why as u go through solution:)\n\nlet\'s consider this subpart :\n```\n(SO4)2\n```\nas we are given after closing paranthesis numbers will appear and what actually this number does?\nit\'s get multiplied to whatever lies inside and it will not affect anything out of the pair open-closed brackets so can we say that \nthe effect of this number will start from closing bracket and eands at opening brackets (u can say otherway also)\nso we can think of balancing paranthesis when we find opposite of one paranthesis we are sure that it belongs to it\'s crossponding pair right. so we use to pop out the stacks top value similarly here can we say \n``` \nthat outer number we can push to stack when and when when we enter into that paranthesis we can multiply the\nstack top value into the count of inner elements and when we enounter \'(\' we know that effect of top element is\nonly till this point so we can pop the top and move on.\n```\n```\npush 2 in stack \nnow go inside \nS = 1 * s.top()\nO = 4 * s.top()\n```\ncome out and pop the stack.\n\nso this way we can deal with series like:\n```\n(SO4)2(PO3)4U3(CO2)3...\n```\nBut what about the neste once ?\nSo the idea here is if their is any outer bracker in which there are brackets and so on... \ni mean nested brackets, but still logic is same still mbers outside of any bracket multiplies to inner one so \nhere insted of just storing values(which lies just after closing bracket) into stack we shuld multiply this with the already top value of stack if any so in this way we can ensure we are taking care of effects of nested brackets.\nand when we see a opening bracket ```(```  we simply pop the top value.\n\nwe can always assume the out input formala have a outer bracket with value one like :\n``` (  Cu2(CO3)3   )1  :>   so we can push one in stack before begining this way we will always have something on the top of stack to multiply with ```\n\n```\nnow taking this example : (( M3(SO4)2 )3 )1\npush one in stack   : top-> 1 \nwe see a \')\'\nnow we get 3 so push 3*s.top in satck : top-> 3,1\nnow we see another \')\' and 2 so push 2*s.top() in stack : top-> 6,3,1\nnow have SO4 so before storing frequency of S and O we need to multiply this with s.top\nS: 1*s.top = 1*6\nO: 4*s.top = 4*6\nnow we see a \'(\' we have to pop the top value of stack : top-> 3,1\nnow we see M3 and we have 3 on the top of stack so \nM: 3*s.top = 3*3\nand after this their is a opening bracker ( so we pop the value 3  \n\nwe are left with 1 on the top of stack  which is okey means we have poped all the brackets.\n``` \n\nso logic wise i hope it clears everything and now \nBe ready to take for the worst part \u2620\uFE0F implimenting these all...\n\njust kidding it\'s easy if u look smartly...\ngo throught the code once \n\n```\nclass Solution {\npublic:\n    \n    \n    \n    string countOfAtoms(string f) \n    {\n        int n=f.size()-1;\n        stack<int> st;\n        map<string, int> mp; // to store the frequency  of elements\n        st.push(1); // assuming a outer bracket with 1\n        string t="0";  // this will store value of intergers we will get,  default value =0;\n        string tem=""; // elemets \n\t\t// as we are moving from back to front everything we will get will be in opposite manner so need to reverse the string before using it\'s values.\n        for(int i=n; i>=0; i--)\n        {\n            char c= f[i];\n          \n            if(isdigit(c)) // if we are encountering digit keep adding \n            {\n                t+=c;\n                tem="";\n            }else if(isalpha(c))// if alphabet\n            {\n                if(isupper(c))// if it is a upper case we need to break bcz on the left of it there will one other elements chars or frequency numbers of  that element. hope u got it.\n                {\n                    reverse(t.begin(),t.end()); // reverse the number \n                    if(t.size()>1) // remeber by default we have t=\'0\' so we need to remove this zero bcz otherwise it will make the number 10x\n                    t.pop_back();\n                    int val= stoi(t);\n                    if(val==0) val=1;  // by default if nothing is written means 1 \n                    tem+=c;\n                    reverse(tem.begin(),tem.end());// reverse the string of elements\n\t\t\t\t\t\n\t\t\t\t\t\n                    if(mp.find(tem)!=mp.end())// add frequency in map \n                       mp[tem]+= st.top()* val;\n                    else\n                       mp[tem]= st.top()*val;\n                    tem="";\n                    t="0";\n                }else  // if it\'s lower case means it can contains more letters so just add the char in tem\n                {\n                    tem+=c;\n                }\n            }else if(c==\')\')  // if we encounter closing bracket we need to push value in to the stack  \n            {\n                reverse(t.begin(),t.end());\n                 if(t.size()>1)\n                    t.pop_back();\n                int val= stoi(t);\n                if(val==0) val=1;\n                st.push(st.top()*val); // multiply before pusing \n                t="0";   // mark number string zero\n                tem="";  // mark element string empty\n\t\t\t\t\n\t\t\t\t\n            }else if(c==\'(\')// pop it\n            {\n                st.pop();\n                t="0";\n                tem="";                 \n            }\n        }\n        string s="";\n        for(auto i: mp)// as map already stores in sorted manner so just hook it together \n        {\n            s+=i.first;\n            if(i.second>1)s+=to_string(i.second);\n            \n        }\n        return s;\n    }\n};\n\n```\n![image](https://assets.leetcode.com/users/images/5cb8a4cf-ebfc-4e15-a728-4c8d3fa3c75d_1720986906.9197927.png)\n\n
['Stack', 'C']