Description

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

Examples

Input:formula = "H2O"
Output:"H2O"
Explanation:

2 hydrogen, 1 oxygen.

Input:Ca(NO3)2
Output:CaN2O6
Explanation:

The formula contains 1 calcium (Ca), and the parentheses (NO3) are multiplied by 2, yielding 2 nitrogen (N) and 6 oxygen (O). Results sorted alphabetically: Ca, N2, O6.

Input:Al2(SO4)3
Output:Al2O12S3
Explanation:

The formula has 2 aluminum (Al), and the parentheses (SO4) are multiplied by 3, resulting in 3 sulfur (S) and 12 oxygen (O). Sorted alphabetically: Al2, O12, S3.

Constraints

  • 1 ≤ formula.length ≤ 1000
  • formula consists of letters, digits, '(', ')'.

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!