Python 4 steps simple O(n) solution
\nclass Solution:\n    def countOfAtoms(self, formula: str) -> str:\n        l=[]\n        # [STEP-1]\n\t\t# Figure the value after each closing bracket\n      
```\nclass Solution:\n    def countOfAtoms(self, formula: str) -> str:\n        l=[]\n        # [STEP-1]\n\t\t# Figure the value after each closing bracket\n        # Count is used to determine how many levels we\'ve gone deep so far. Ex => ((x)), depth of x=2 here \n        i=0\n        count=0\n        while(i<len(formula)):\n            if(formula[i]=="("):\n                count+=1\n                i+=1\n            elif(formula[i]==")"):\n                currIndex=i\n                i+=1\n                digi=""\n                while(i<len(formula) and formula[i].isdigit()):\n                    digi+=formula[i]\n                    i+=1\n                if(digi==""):digi="1"\n                l.append([currIndex,count,int(digi)])\n                count-=1\n            else:i+=1\n         \n\t\t# [STEP-2]\n        # At each depth, multiply the value with depth-1 value.\n        # ((x)2)3 => here depths would be {depth1: 3, depth2: 3}\n        # so for each depth, we\'to mutliply the value with outmost depth, like for depth 2, we\'ve to mutliply it\'s value with depth1...\n        dCounts=defaultdict(int)\n        dCounts[0]=1\n        for i in range(len(l)-1,-1,-1):\n            l[i][2]*=dCounts[l[i][1]-1]\n            dCounts[l[i][1]]=l[i][2]\n        \n\t\t# [STEP-3]\n        # Now, just for each element, figure out it\'s count and multiply it with it\'s depth count\n        # ((x2)3)4, here depth counts woud be {depth1:4, depth2:12 (which is 4*3)} and the depth of x is 2, so mutliply value of x (i.e.., 2) with it\'s depth count (i.e.., depth2=12) which becomes 24 \n\t\ti=0\n        count=0\n        dEle=defaultdict(int)\n        l=l[::-1] #reversing just to make it easier to pop. (or dequeue can be used here as well)\n        while(i<len(formula)):\n            while(len(l)>0 and l[-1][0]<i):l.pop() # popping because we\'ve crossed that index\n            if(formula[i]=="("):\n                count+=1\n                i+=1\n            elif(formula[i]==")"):\n                i+=1\n                while(i<len(formula) and formula[i].isdigit()):i+=1\n                count-=1\n            else:\n                ele=formula[i]\n                i+=1\n                while(i<len(formula) and formula[i].isalpha() and formula[i].islower()):\n                    ele+=formula[i]\n                    i+=1\n                digi=""\n                while(i<len(formula) and formula[i].isdigit()):\n                    digi+=formula[i]\n                    i+=1\n                if(len(digi)==0):digi="1"\n                if(count==0):dEle[ele]+=int(digi)\n                else:\n                    currCount=int(digi)\n                    for j in range(len(l)-1,-1,-1):\n                        if(l[j][1]==count):\n                            currCount*=l[j][2]\n                            break\n                    dEle[ele]+=currCount\n        \n\t\t# [STEP-4]\n        #sort the dictionary and append count of each ele.\n        res=""\n        for i in sorted(dEle):\n            res+=i\n            if(dEle[i]>1):res+=str(dEle[i])\n        return res\n```
['Python3']