Update appNew update is available. Click here to update.
Last Updated: 4 Nov, 2020
Number of Atoms
Moderate
Problem statement

Given a string, representing a chemical formula, return the count of each atom.

An atomic element always starts with an uppercase character, then zero or more lowercase letters follow, representing the name.

One or more digits representing that element's count may follow if the count is greater than 1. If the count is 1, no digits will follow. For example, H2O and H2O2 are possible, but H1O2 is impossible.

Two formulas can be concatenated together to produce another formula. For example, H2O2He3Mg4 is also a formula.

A formula placed in parentheses, and a count (optionally added) is also a formula. For example, (H2O2) and (H2O2)3 are formulas.

Given a formula, return the count of all elements as a string in the following form: the first name (in sorted order), followed by its count (if that count is more than 1), followed by the second name (in sorted order), followed by its count (if that count is more than 1), and so on.

Note :
The given formula consists of English alphabets, digits, β€˜(β€˜ and ’)’.

The given formula is always valid. 
Input Format
The first line of input contains an integer 'T' representing the number of test cases. Then the test cases follow.

The only line of each test case contains a string representing the formula.
Output Format:
For each test case, return the string representing the count of all the elements is printed.

The output for each test case is in a separate line.
Note:
You are not required to print the expected output, it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 10
1 <= |S| <= 10^4

Time Limit: 1 sec
Note :
The count of each element in the formula will not exceed 2^31.
Approaches

01Approach

The main idea behind this approach is to use recursion and find the count of atoms between a β€˜(β€˜ and a β€˜)’. Whenever we encounter a β€˜(β€˜, we make a recursive call to the function, which after execution, gives an unordered map consisting of the count of atoms within the current formula. Then, we can use that map to calculate the count of atoms in a string containing that parenthesis.
 

Following is the algorithm for this approach :
 

  1. If we encounter alphabets/atoms, then add them to a string. If any lowercase letters follow, then append them to the string as well.
  2. If we encounter digits, then build the total value of the current number.
  3. After processing the alphabet and digit, add it to a UNORDERED_MAP<STRING, INT>
  4. If the Atom already exists in the map then add the digit to the existing value else just insert the atom with the digit.
  5. If we encounter '(' then recursively call the function with a new UNORDERED_MAP<STRING, INT> and repeat steps 1 to 4.
  6. Once we return from the recursive call process with the new map, add its <ATOM, VALUE> pairs to the existing map.
  7. Whenever we encounter ')', we will check if there is a digit following ')'. If yes, then we multiply that digit with all the values in the map.
  8. Now, we have the count of each element in the final map. Push all the elements and their respective counts from the map to a vector/list/array. Since we need the elements in sorted order, we sort these elements and then build a string from the final sorted structure.