Aggregate Element Counts in Nested Formulas

Quick Overview

Aggregate Element Counts in Nested Formulas. Parse a valid chemical formula. An element name begins with an uppercase English letter followed by zero or more lowercase letters.

Aggregate Element Counts in Nested Formulas

Company: Agoda

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Parse a valid chemical formula. An element name begins with an uppercase English letter followed by zero or more lowercase letters. A following decimal count greater than one multiplies that element; an omitted count means one. Concatenation combines formulas, and a parenthesized formula may have its own optional multiplier. Return element names in lexicographic order, each followed by its total count only when that total exceeds one. Output counts fit signed 32-bit integers. ```cpp class Solution { public: string countOfAtoms(string formula); }; ``` ### Examples **Example 1:** ```text formula = "Na2(CO3)2" Output: "C2Na2O6" Explanation: The group contributes two C atoms and six O atoms, with two Na atoms outside it. ``` **Example 2:** ```text formula = "He(He2O)3" Output: "He7O3" Explanation: He totals 1+2*3=7 and O totals 3. ``` ### Constraints - Formula length is 1 through 1000. - Characters are English letters, decimal digits or parentheses. - The grammar is valid, and every final element count fits signed 32-bit int. ### Contract considerations Preserve sorted element names, omitted unit counts and the original output-fit guarantee. ```hint Read the boundary condition What value can a recursive parse of one parenthesized group return? ``` ### Boundary reminders Preserve sorted element names, omitted unit counts and the original output-fit guarantee.

Overview: Aggregate Element Counts in Nested Formulas. Parse a valid chemical formula. An element name begins with an uppercase English letter followed by zero or more lowercase letters.

Agoda logo
Agoda
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Parse a valid chemical formula. An element name begins with an uppercase English letter followed by zero or more lowercase letters. A following decimal count greater than one multiplies that element; an omitted count means one. Concatenation combines formulas, and a parenthesized formula may have its own optional multiplier. Return element names in lexicographic order, each followed by its total count only when that total exceeds one. Output counts fit signed 32-bit integers.

class Solution {
public:
    string countOfAtoms(string formula);
};

Examples

Example 1:

formula = "Na2(CO3)2"
Output: "C2Na2O6"
Explanation: The group contributes two C atoms and six O atoms, with two Na atoms outside it.

Example 2:

formula = "He(He2O)3"
Output: "He7O3"
Explanation: He totals 1+2*3=7 and O totals 3.

Constraints

  • Formula length is 1 through 1000.
  • Characters are English letters, decimal digits or parentheses.
  • The grammar is valid, and every final element count fits signed 32-bit int.

Contract considerations

Preserve sorted element names, omitted unit counts and the original output-fit guarantee.

Boundary reminders

Preserve sorted element names, omitted unit counts and the original output-fit guarantee.