Trains the technique from
LeetCode 726Number of AtomsThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A furniture workshop writes the contents of a flat-pack kit in a compact notation, given to you as formula.
"K", "Nut" or "Bba"."Nut2" means two nuts."(BoltNut)3" packs three bolts and three nuts. A bracketed run with no number after it is packed once.Write the kit's total contents in the workshop's tidy form: every distinct part code exactly once, the codes in dictionary order, and each code followed immediately by its total quantity. A quantity of exactly one is left off, so a kit holding one hinge and five screws is written "HingeScrew5".
Return the tidy form of formula as a string. Every number written in the notation is at least 2 and carries no leading zero, and formula is always well formed.
Example 1
The bracketed run is packed three times, giving three bolts and three nuts, and one washer follows it. In dictionary order the codes are Bolt, Nut, Washer, and the washer's quantity of one is left off.
Example 2
The inner run holds two cams and one pin and is packed three times, so six cams and three pins, joined by one rod. All of that is packed twice, giving twelve cams, six pins and two rods.
Example 3
The notation names one B and two Ab. Dictionary order puts Ab before B, and B's quantity of one is left off.
Example 4
Two Ab sit inside the brackets and the run is packed twelve times, so the kit holds twenty-four of them.
Example 5
A single part code with no number and no brackets, so the kit holds one K and the quantity is left off.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def count_of_atoms(formula: str) -> str:public String countOfAtoms(String formula)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.