Chemical Formula Problem Statement

Given a chemical formula as a string, return the count of each atom present in the compound.

Explanation:

  • An atomic element starts with an uppercase letter followed by zero or more lowercase letters.
  • Element counts follow as one or more digits if greater than 1. No digits imply a count of 1.
  • Formulas can be nested within parentheses, optionally followed by a multiplier.

Input:

The first line contains an integer 'T' (number of test cases). The next 'T' lines each contain a string representing a chemical formula.

Output:

Return the count of all elements for each test case as a string, sorted by element name. Include count if greater than 1. Each test case result should be on a new line.

Example:

Input:
1
C6H2(Cl3(OH2)3)3
Output:
C6Cl9H20O9

Constraints:

  • 1 <= T <= 10
  • 1 <= |S| <= 104
  • Time Limit: 1 sec

Note:

  • Element counts will not exceed 231.
Be the first one to answer
Add answer anonymously...
Amazon Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter