Tap here to switch tabs
Problem
Submissions

Problem: Number of Atoms

med
30 min
Try to solve the Number of Atoms problem.

Statement

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

An atomic element always begins with an uppercase letter, optionally followed by zero or more lowercase letters.

One or more digits representing that element’s count may follow if the count is greater than 11. If the count is equal to 11, no digits will follow.

A formula placed inside parentheses followed by a count (also optionally omitted if equal to 11) is also a valid formula.

Return the total count of each atom as a string where elements are sorted in lexicographical order, followed by their count (if greater than 11), followed by the next element name in sorted order, and so on.

Constraints:

  • 1≤1 \leq formula.length ≤1000\leq 1000

  • formula consists of English letters, digits, '(', and ')'.

  • formula is always valid.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Number of Atoms

med
30 min
Try to solve the Number of Atoms problem.

Statement

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

An atomic element always begins with an uppercase letter, optionally followed by zero or more lowercase letters.

One or more digits representing that element’s count may follow if the count is greater than 11. If the count is equal to 11, no digits will follow.

A formula placed inside parentheses followed by a count (also optionally omitted if equal to 11) is also a valid formula.

Return the total count of each atom as a string where elements are sorted in lexicographical order, followed by their count (if greater than 11), followed by the next element name in sorted order, and so on.

Constraints:

  • 1≤1 \leq formula.length ≤1000\leq 1000

  • formula consists of English letters, digits, '(', and ')'.

  • formula is always valid.