Number of Atoms

Given a string representing a chemical formula, return the count of each atom. The atomic element always starts with an uppercase character, possibly followed by lowercase letters, and may have digits indicating its count. If the count is 1, no digits will follow. Formulas can be concatenated or placed in parentheses with an optional multiplier. Return the counts in sorted order as a string.

Constraints

  • 1 <= formula.length <= 1000
  • formula consists of English letters, digits, '(', and ')'.
  • formula is always valid.
Saving… · 3 lines
class Solution:
def countOfAtoms(self, formula: str) -> str:

Console

Click Run to execute sample tests.