shlogg · Early preview
Md Ariful Haque @mah-shamim

Number Of Atoms In Chemical Formula

Parse chemical formula, count atoms, and return sorted string of elements with counts (if > 1). Example: "H2O" -> "H2O", "Mg(OH)2" -> "H2MgO2".

726. Number of Atoms
Hard
Given a string formula representing a chemical formula, return the count of each atom.
The atomic element always starts with an uppercase character, then zero or more lowercase letters, representing the name.
One or more digits representing that element's count may follow if the count is greater than 1. If the count is 1, no digits will follow.

For example, "H2O" and "H2O2" are possible, but "H1O2" is impossible.

Two formulas are concatenated together to produce another formula.

For example, "H2O2He3Mg4" is also a formula.

A formula placed in parentheses, and a co...