Guesswork || Trash Code || LoL
Intuition\nKept on debugging it and finally got AC in one hour somehow.\n\n# Approach\nFound all pairs of () using stack and then did what Hint 1 said.\n\n# Com
# Intuition\nKept on debugging it and finally got AC in one hour somehow.\n\n# Approach\nFound all pairs of () using stack and then did what Hint 1 said.\n\n# Complexity\n- Time complexity:\nIDC\n\n- Space complexity:\nIDC\n\n# Code\n```\nclass Solution {\npublic:\n    void solve(string s,map<string,int>&m,unordered_map<int,int>&pos,int idx, int k){\n        int n = s.size();\n        if(idx>=n) return;\n        if(s[idx]==\'(\'){\n            int j = pos[idx];\n            if(j<n-1&&s[j+1]<=\'9\'&&s[j+1]>=\'0\'){\n                while(j+1<n&&s[j+1]<=\'9\'&&s[j+1]>=\'0\') j++;\n                int val = stoi(s.substr(pos[idx]+1,j-pos[idx]));\n                solve(s,m,pos,idx+1,k*val);\n                solve(s,m,pos,pos[idx]+1,k);\n            }\n            else {\n                solve(s,m,pos,idx+1,k);\n                solve(s,m,pos,pos[idx]+1,k);\n            }\n        }\n        else {\n            int i = idx;\n            cal(s,m,i,k);\n            if(s[i]<=\'Z\'&&s[i]>=\'A\') solve(s,m,pos,i,k);\n            else if(s[i]==\'(\') solve(s,m,pos,i,k);\n        }\n    }\n\n    void cal(string s,map<string,int>&m, int &i, int k){\n        int n = s.size();\n        if(i>=n) return;\n        int j = i; bool f=0;\n        while(i<n&&((s[i]<=\'Z\'&&s[i]>=\'A\')||(s[i]<=\'z\'&&s[i]>=\'a\'))) {\n            if(s[i]<=\'Z\'&&s[i]>=\'A\') {\n                if(!f) f=1;\n                else break;\n            }\n            i++;\n        }\n        string elem;\n        if(i==n||s[i]==\'(\'){\n            elem = s.substr(j,i-j);\n            m[elem]+=k; return;\n        }\n        elem = s.substr(j,i-j); j = i;\n        while(i<n&&s[i]<=\'9\'&&s[i]>=\'0\') i++;\n        if(i==j){\n            if(s[j]<=\'9\'&&s[j]>=\'0\'){\n                int val = stoi(s.substr(j,1));\n                m[elem]+=val*k;\n            }\n            else m[elem]+=k;\n        }\n        else{\n            if(i==n){\n                int val = stoi(s.substr(j));\n                m[elem]+=val*k;\n            }\n            else{\n                int val = stoi(s.substr(j,i-j));\n                m[elem]+=val*k;\n            }\n        }\n\n    }\n    string countOfAtoms(string s) {\n        int n = s.size();\n        map<string,int>m; int k =1;int idx = 0;\n        unordered_map<int,int>pos;\n        vector<int>v;\n        for(int i=0;i<n;i++){\n            if(s[i]==\'(\') v.push_back(i);\n            if(s[i]==\')\'){\n                int j = v.back(); v.pop_back();\n                pos[j] = i;\n            }\n        }\n        solve(s,m,pos,idx,1);\n        string res;\n        for(auto p:m){\n            res+=p.first;\n            if(p.second>1) res+=to_string(p.second);\n        }\n        int ind = -1;\n        for(int i=0;i<res.size();i++){\n            if(res[i]<=\'9\'&&res[i]>=\'0\') ind=i;\n            else break;\n        }\n        return res.substr(ind+1);\n    }\n};\n```
['Hash Table', 'String', 'Stack', 'C++']