Skip to content
Problem 726 · LeetCode 726(opens in a new tab)

Number of Atoms

HardHash tables

A chemical formula is written as a string. Each element name is one capital letter followed by zero or more small letters, like C or Ca. A number right after a name says how many of that atom there are; with no number, there is one. A part of the formula can be wrapped in parentheses and followed by a number, which multiplies everything inside. Groups can nest, and the same element can show up in several places.

Return the total count of each element as one string: the element names in sorted order, each followed by its count, with the count left out when it is 1.

Example 1

Input:  formula = "CaCO3"
Output: "CCaO3"
Why:    one C, one Ca, three O; C sorts before Ca
2 more examples and the constraints

Example 2

Input:  formula = "Ca(OH)2"
Output: "CaH2O2"
Why:    the 2 doubles both O and H inside the parentheses

Example 3

Input:  formula = "Fe2(SO4)3"
Output: "Fe2O12S3"
Why:    S is 1 × 3, O is 4 × 3, Fe stays 2

Constraints

  • 1 ≤ length of formula ≤ 1,000
  • The formula holds letters, digits and parentheses, and is always valid
  • No number is 1 or starts with 0
  • Every count in the answer fits in a 32-bit signed integer

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Stack of count mapsO(n²)
  2. Right to left with a multiplier stackO(n + k log k)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 1,000 characters, deeply nestedVerdict
Stack of count mapsO(n²)
Passes; folds maps per level
Right to left with a multiplier stackO(n + k log k)
Best; each atom added once

n = length of the formula, k = number of distinct element names.

Edge cases to test

  • Names of two letters. In CaCO3, Ca and C are different elements; C sorts first.
  • Numbers of several digits. C12H22O11 must read 12, not 1 then 2.
  • A group with no number. (H2O) multiplies by 1.
  • The same element twice. HOH gives H2O: add the counts together.
  • Deep nesting. Thirty groups each times 2 around one H give 1,073,741,824.

Common mistakes

  • Treating each capital and small letter as its own element, so Ca becomes C and a.
  • Reading only one digit of a count like 12.
  • Forgetting to leave out the count when it is 1, or writing the names unsorted.

How to talk through it

  1. 1Clarify. Can groups nest? Can an element appear twice? Is a count of 1 written?
  2. 2Give the stack of maps. One map per open group; fold it in, multiplied, when the group closes.
  3. 3Notice the reverse trick. Read backwards and the multiplier comes first, so a stack of products is enough.
  4. 4Code the scan. Digits and small letters go in front; a capital finishes a name; parentheses push and pop.
  5. 5Test out loud. Run Ca(OH)2, a two-digit count and a nested group before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?