Program to parse the Molecules and get the Atoms count

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 :





// Java program to parse the molecule
// and get the atoms count
import java.util.*;
class GFG {
    static void getCount(String str)
        // Use LinkedHashmap to store
        // elements in insertion oreder
        Map<String, Integer> mp
            = new LinkedHashMap<String, Integer>();
        for (int i = 0; i < str.length(); i++) {
            int count = 0;
            // Convert the string element into character
            char c = str.charAt(i);
            String a = " ";
            // Convert the character element
            // into string element
            a = String.valueOf(c);
            // Check string contains the Capital A - Z value.
            if (a.matches("[A-Z]")) {
                for (int j = i + 1; j < str.length(); j++) {
                    char d = str.charAt(j);
                    String b = String.valueOf(d);
                    // Check string contains the small a-z value.
                    if (b.matches("[a-z]")) {
                        a += b;
                        if (mp.get(a) == null)
                            mp.put(a, 1);
                            mp.put(a, mp.get(a) + 1);
                        count = 1;
                    // Check string contains the number value.
                    else if (b.matches("[\\d]")) {
                        int k = Integer.parseInt(b);
                        mp.put(a, k);
                        count = 1;
                    else {
                        i = j - 1;
                if (count == 0) {
                    if (mp.get(a) == null)
                        mp.put(a, 1);
                        mp.put(a, mp.get(a) + 1);
        System.out.println("\nAtom count:");
        for (Map.Entry<String, Integer>
                 entry : mp.entrySet())
                               + " "
                               + entry.getValue());
    // Driver code
    public static void main(String[] args)
        String str = "Fe2H3OH";
        System.out.println("Given molecule: "
                           + str);



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.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.