Given a chemical formula as a string, the task is to get the count of atoms in this chemical formula.
Input: str = "Fe2H3OH" Output: Fe 2 H 4 O 1 Input: str = "NaCl2NaO2" Output: Na 2 Cl 2 O 2
Approach: The below approach work in Java programming language:
- Take LinkedHashMap to store atom name(Key) and count(value) in insertion order.
- Check if the string character is lowercase then add to the previous uppercase string character.
- Check if the string contains the number then add to the count(value) to their particular atom name(key).
- Print the atom name(key) as well as count(value).
Below is the implementation of the above approach :
Given molecule: Fe2H3OH Atom count: Fe 2 H 4 O 1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program to Parse a comma separated string in C++
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Check if count of Alphabets and count of Numbers are equal in the given String
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count of binary strings of length N having equal count of 0's and 1's
- Program to count vowels in a string (Iterative and Recursive)
- Program to count vowels, consonant, digits and special characters in string.
- Program to count number of distinct Squares and Cubes upto N
- Python program to count Even and Odd numbers in a List
- Python program to count positive and negative numbers in a list
- C program to count number of vowels and consonants in a String
- C program to count Positive and Negative numbers in an Array
- C program to count zeros and ones in binary representation of a number
- Find if it is possible to get a ratio from given ranges of costs and quantities
- How to get Day, Month and Year from Date in Java
- Minimize the number of replacements to get a string with same number of 'a', 'b' and 'c' in it
- Check if count of even divisors of N is equal to count of odd divisors
- Count ways to split a Binary String into three substrings having equal count of zeros
- Program to count occurrence of a given character in a string
- Program to Count numbers on fingers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.