Calculate the weight of a molecule from its formula.
Unlock detailed company stats for this questionUpgrade
Calculate the weight of a molecule that contains only C, H and O, with weights 12, 1 and 8 respectively. The formula contains only C, H, O and digits; multi-atom counts are a single digit, for example CH4. Can be done with two pointers; a stack is not really needed.
Follow-up: formulas can now contain brackets, and the number outside a bracket multiplies the weight of the molecule inside, e.g. H(CH4)2 = 33. A stack is needed now.
Expect follow-ups like
- Now the formula can contain brackets, and a number outside a bracket multiplies the enclosed molecule's weight, for example H(CH4)2 = 33.
Format · 60-minute HackerRank live coding with interviewer.
Community answers
No answers contributed by the community yet.
Related questions
Compute molecular weight from a chemical formula.Calculate the height of a binary tree.Calculate all of the possible knight moves on a chess board.Related courses

Course

Course
Course
