faster than 100% | easiest solution in c++ | no stack used | explained solution
I have used a vector which has a pair value of string and int. In the String i am storing the value of the element and in int i am storing the frequency. I am t
I have used a vector which has a pair value of string and int. In the String i am storing the value of the element and in int i am storing the frequency. I am taking into consideration that the length of the element can be greater than 1 like "Mg" or "Fe" so the intution is that once i get a Capital letter then i will keep looking the the next index value which is not a capital value or a special character or a digit. then i will be appending my entire string in the pair and by default i will set the int value as 1.\n\nIf i get an Opening bracket or a closing bracket i will just make the pair of it as **("(",1)** or **(")",1)** this 1 in the int part of the pair means that it has not been processed and if i encounter a digit just after my closing bracket i will keep on iterating till i find a pair where there is an opening bracket and the int value is 1 because it will be an indication that this value has not got processed and when i find this particular pair i will just change the value of the int to whatever was there outside the bracket like \n(OH)2\nso it will become \n(**"(",2**)   ,    (**"O",2**)  ,  (**"H",2**)  ,  (**")",2**)\n\nand here the 2 for the brackets indicates that this sequence has already been processed.\n\nand then i just used a hashmap and calculated the entire value of the pair vector.\n\nlets just say the eg was \n\nK4(ON(SO3)2)2\n\nso this will be put in my pair vector like\n\n("K",4)  ,  ("(",2)  ,   ("O",2)  ,  ("N",2)  ,  ("(",4)  ,  ("S",4)  ,  ("O",12)  ,  (")",4)  ,  (")",2)\n\n\nand with this value in my vector i can easy create my hashmap with my required value like\n\nK : 4 \nN : 2 \nO : 14\nS : 4\n\nand with this value i will just append it in my string.\n\n\n```\n string countOfAtoms(string formula) {\n       vector<pair<string,int>> v;\n        map<string,int> mp;\n        int s=formula.size();\n        int len=-1;\n        for(int i=0;i<formula.length();i++)\n        {\n            if(isupper(formula[i]))\n            {\n                pair<string,int> temp;\n                string t=string(1,formula[i]);\n                while(i<s-1 && islower(formula[i+1]))\n                {\n                    t+=formula[i+1];\n                    i++;\n                }\n                temp.first=t;\n                temp.second=1;\n                v.push_back(temp);\n                len++;\n                \n            }\n            else if(formula[i]==\'(\' || formula[i]==\')\')\n            {\n                pair<string,int>temp;\n                temp.first=formula[i];\n                temp.second=1;\n                v.push_back(temp);\n                len++;\n            }\n            else if(isdigit(formula[i]))\n            {\n                string p=string(1,formula[i]);\n                while(i<s-1 && isdigit(formula[i+1]))\n                {\n                    p+=formula[i+1];\n                    i++;\n                }\n                if(v[len].first==")")\n                {\n                    int x=len;\n                    \n                    while(true)\n                    {\n                        if(v[x].first=="(" && v[x].second==1)\n                        {\n                            \n                            v[x].second*=stoi(p);\n                            break;\n                        }\n                        v[x].second*=stoi(p);\n                        x--;\n                    }\n                    \n                }\n                else\n                {\n                    v[len].second*=stoi(p);\n                }\n                \n            }\n        }\n        for(int i=0;i<=len;i++)\n        {\n            if(v[i].first!="(" && v[i].first!=")"){\n            \n            mp[v[i].first]+=v[i].second;\n            }\n        }\n        string ans="";\n        for(auto i:mp)\n        {\n            ans+=i.first;\n            if(i.second!=1){\n            ans+=to_string(i.second);}\n        }\n        return ans;\n    }\n```\n\n\nplease Upvote if you liked my solution \n\n**#happy_coding**
['C', 'C++']