|
| 1 | +/** |
| 2 | + * 726. Number of Atoms |
| 3 | + * https://leetcode.com/problems/number-of-atoms/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * Given a string formula representing a chemical formula, return the count of each atom. |
| 7 | + * |
| 8 | + * The atomic element always starts with an uppercase character, then zero or more lowercase |
| 9 | + * letters, representing the name. |
| 10 | + * |
| 11 | + * One or more digits representing that element's count may follow if the count is greater |
| 12 | + * than 1. If the count is 1, no digits will follow. |
| 13 | + * - For example, "H2O" and "H2O2" are possible, but "H1O2" is impossible. |
| 14 | + * |
| 15 | + * Two formulas are concatenated together to produce another formula. |
| 16 | + * - For example, "H2O2He3Mg4" is also a formula. |
| 17 | + * |
| 18 | + * A formula placed in parentheses, and a count (optionally added) is also a formula. |
| 19 | + * - For example, "(H2O2)" and "(H2O2)3" are formulas. |
| 20 | + * |
| 21 | + * Return the count of all elements as a string in the following form: the first name (in sorted |
| 22 | + * order), followed by its count (if that count is more than 1), followed by the second name |
| 23 | + * (in sorted order), followed by its count (if that count is more than 1), and so on. |
| 24 | + * |
| 25 | + * The test cases are generated so that all the values in the output fit in a 32-bit integer. |
| 26 | + */ |
| 27 | + |
| 28 | +/** |
| 29 | + * @param {string} formula |
| 30 | + * @return {string} |
| 31 | + */ |
| 32 | +var countOfAtoms = function(formula) { |
| 33 | + const [counts] = parseFormula(formula, 0); |
| 34 | + return [...counts].sort().map(([atom, count]) => atom + (count > 1 ? count : '')).join(''); |
| 35 | + |
| 36 | + function parseFormula(str, index) { |
| 37 | + const atomCounts = new Map(); |
| 38 | + while (index < str.length && str[index] !== ')') { |
| 39 | + if (str[index] === '(') { |
| 40 | + const [subCounts, nextIndex] = parseFormula(str, index + 1); |
| 41 | + const [multiplier, updatedIndex] = extractNumber(str, nextIndex + 1); |
| 42 | + for (const [atom, count] of subCounts) { |
| 43 | + atomCounts.set(atom, (atomCounts.get(atom) || 0) + count * (multiplier || 1)); |
| 44 | + } |
| 45 | + index = updatedIndex; |
| 46 | + } else { |
| 47 | + const [atom, nextIndex] = extractAtom(str, index); |
| 48 | + const [count, updatedIndex] = extractNumber(str, nextIndex); |
| 49 | + atomCounts.set(atom, (atomCounts.get(atom) || 0) + (count || 1)); |
| 50 | + index = updatedIndex; |
| 51 | + } |
| 52 | + } |
| 53 | + return [atomCounts, index]; |
| 54 | + } |
| 55 | + |
| 56 | + function extractAtom(str, index) { |
| 57 | + let atom = str[index]; |
| 58 | + while (++index < str.length && /[a-z]/.test(str[index])) atom += str[index]; |
| 59 | + return [atom, index]; |
| 60 | + } |
| 61 | + |
| 62 | + function extractNumber(str, index) { |
| 63 | + let number = 0; |
| 64 | + while (index < str.length && /[0-9]/.test(str[index])) { |
| 65 | + number = number * 10 + Number(str[index++]); |
| 66 | + } |
| 67 | + return [number || null, index]; |
| 68 | + } |
| 69 | +}; |
0 commit comments