726. Number of Atoms #27
-
Given a string 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
Two formulas are concatenated together to produce another formula.
A formula placed in parentheses, and a count (optionally added) is also a formula.
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 The test cases are generated so that all the values in the output fit in a Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Here's the step-by-step approach:
Here is the complete solution in PHP: 726. Number of Atoms <?php
function countOfAtoms($formula) {
$len = strlen($formula);
$i = 0;
$stack = [];
$counter = [];
while ($i < $len) {
if ($formula[$i] === '(') {
array_push($stack, $counter);
$counter = [];
$i++;
} elseif ($formula[$i] === ')') {
$i++;
$multiplier = 0;
while ($i < $len && is_numeric($formula[$i])) {
$multiplier = $multiplier * 10 + intval($formula[$i]);
$i++;
}
if ($multiplier == 0) {
$multiplier = 1;
}
foreach ($counter as $atom => $count) {
$counter[$atom] = $count * $multiplier;
}
$topCounter = array_pop($stack);
foreach ($counter as $atom => $count) {
if (isset($topCounter[$atom])) {
$topCounter[$atom] += $count;
} else {
$topCounter[$atom] = $count;
}
}
$counter = $topCounter;
} else {
$start = $i;
$i++;
while ($i < $len && ctype_lower($formula[$i])) {
$i++;
}
$atom = substr($formula, $start, $i - $start);
$start = $i;
while ($i < $len && is_numeric($formula[$i])) {
$i++;
}
$count = $i > $start ? intval(substr($formula, $start, $i - $start)) : 1;
if (isset($counter[$atom])) {
$counter[$atom] += $count;
} else {
$counter[$atom] = $count;
}
}
}
ksort($counter);
$result = '';
foreach ($counter as $atom => $count) {
$result .= $atom;
if ($count > 1) {
$result .= $count;
}
}
return $result;
}
// Test cases
echo countOfAtoms("H2O") . "\n"; // Output: "H2O"
echo countOfAtoms("Mg(OH)2") . "\n"; // Output: "H2MgO2"
echo countOfAtoms("K4(ON(SO3)2)2") . "\n"; // Output: "K4N2O14S4"
?> Explanation of Key Parts:
This approach ensures that the formula is parsed correctly even with nested parentheses and multipliers, and that the counts are accurate and correctly sorted. |
Beta Was this translation helpful? Give feedback.
Here's the step-by-step approach:
Initialize a Stack and a Counter: Use a stack to keep track of the current context of counts when parsing nested structures. The counter will help us store the counts of atoms at the current level.
Iterate Over the Formula: Traverse the formula string character by character.
Handle Parentheses:
'('
, push the current counter onto the stack and reset the counter for the new context.')'
, finalize the current counter, then look ahead to find the multiplier, and pop the previous counter from the stack to merge the counts.Handle Atoms and Multipliers: