Package g0701_0800.s0726_number_of_atoms
Class Solution
java.lang.Object
g0701_0800.s0726_number_of_atoms.Solution
726 - Number of Atoms.<p>Hard</p>
<p>Given a string <code>formula</code> representing a chemical formula, return <em>the count of each atom</em>.</p>
<p>The atomic element always starts with an uppercase character, then zero or more lowercase letters, representing the name.</p>
<p>One or more digits representing that element’s count may follow if the count is greater than <code>1</code>. If the count is <code>1</code>, no digits will follow.</p>
<ul>
<li>For example, <code>"H2O"</code> and <code>"H2O2"</code> are possible, but <code>"H1O2"</code> is impossible.</li>
</ul>
<p>Two formulas are concatenated together to produce another formula.</p>
<ul>
<li>For example, <code>"H2O2He3Mg4"</code> is also a formula.</li>
</ul>
<p>A formula placed in parentheses, and a count (optionally added) is also a formula.</p>
<ul>
<li>For example, <code>"(H2O2)"</code> and <code>"(H2O2)3"</code> are formulas.</li>
</ul>
<p>Return the count of all elements as a string in the following form: the first name (in sorted order), followed by its count (if that count is more than <code>1</code>), followed by the second name (in sorted order), followed by its count (if that count is more than <code>1</code>), and so on.</p>
<p>The test cases are generated so that all the values in the output fit in a <strong>32-bit</strong> integer.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> formula = “H2O”</p>
<p><strong>Output:</strong> “H2O”</p>
<p><strong>Explanation:</strong> The count of elements are {‘H’: 2, ‘O’: 1}.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> formula = “Mg(OH)2”</p>
<p><strong>Output:</strong> “H2MgO2”</p>
<p><strong>Explanation:</strong> The count of elements are {‘H’: 2, ‘Mg’: 1, ‘O’: 2}.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> formula = “K4(ON(SO3)2)2”</p>
<p><strong>Output:</strong> “K4N2O14S4”</p>
<p><strong>Explanation:</strong> The count of elements are {‘K’: 4, ‘N’: 2, ‘O’: 14, ‘S’: 4}.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= formula.length <= 1000</code></li>
<li><code>formula</code> consists of English letters, digits, <code>'('</code>, and <code>')'</code>.</li>
<li><code>formula</code> is always valid.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
countOfAtoms
-