← 返回 meta 的题目列表Number of Atoms
类型:online_judge
Given a chemical formula (e.g. 'H2O', 'Mg(OH)2', 'K4(ON(SO3)2)2'), compute the number of each atom. The formula may contain parentheses, and the numbers following them indicate the number of times the elements inside the parentheses are multiplied. Please implement a function to parse such a formula and return the number of each type of atom. You can assume all chemical formulas are valid. The input is a string, and the output is a string that returns atoms and their counts in lexicographical order. Test cases include: 'H2O', 'Mg(OH)2', 'K4(ON(SO3)2)2', and other complex and edge cases.
Example
Input
H2O