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
- Program to Count numbers on fingers
- Program to count occurrence of a given character in a string
- Program to count digits in an integer (4 Different Methods)
- Python program to count Even and Odd numbers in a List
- Program to find the count of coins of each type from the given ratio
- Java program to count the characters in each word in a given sentence
- Program to count vowels in a string (Iterative and Recursive)
- Python program to count positive and negative numbers in a list
- Program to count number of distinct Squares and Cubes upto N
- Python program to count number of vowels using sets in given string
- Program to count vowels, consonant, digits and special characters in string.
- Longest subarray having count of 1s one more than count of 0s
- Count numbers that don't contain 3
- Count all pairs with given XOR
- Count of quadruplets with given Sum | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.