- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Number of Atoms in C++
Suppose we have a chemical formula; we have to find the count of each atom.
An atomic element will always start with an uppercase character, there can be zero or more lowercase letters, representing the name. And 1 or more digits representing the count of that element may follow if the count is greater than 1. But if the count is 1, no digits will follow. As an example, H2O and H2O2 both are valid, but H1O2 is invalid.
So, if the input is like Na2(CO)3, then the output will be C3Na2O3, so this indicates 3 Carbon (C), 2 Sodium (Na), 3 Oxygen (O).
To solve this, we will follow these steps −
Define a function makeRet(), this will take one map m,
ret := blank string
for each key-value pair 'it' in m −
ret := ret + key of it
if value of it > 1, then −
ret := ret + value of it as string
return ret
Define a function countOfAtoms(), this will take s,
Define one map m
Define one stack st
i := 0, n := size of s
while i < n, do −
c := s[i]
(increase i by 1)
if c is same as '(', then −
insert m into st
m := Define one map
otherwise when c is same as ')', then −
val := 0
while (i < n and s[i] in range 0 to 9), do −
val := val * 10 + (s[i] - ASCII of '0')
(increase i by 1)
Define one map temp := top element of st
delete element from st
for each key-value pair 'it' in m −
value of it := value of it * val
temp[key of it] := temp[key of it] + value of it
m := temp
Otherwise
name := blank string
val := 0
name := name + c
while (i < n and s[i] in range 'a' to 'z'), do −
name := name + s[i]
(increase i by 1)
while (i < n and s[i] in range 0 to 9), do −
val := val * 10 + (s[i] - ASCII of '0')
(increase i by 1)
val := (if val is same as 0, then 1, otherwise val)
m[name] := m[name] + val
return makeRet(m)
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: string makeRet(map<string, int> m){ string ret = ""; for (auto& it : m) { ret += it.first; if (it.second > 1) { ret += to_string(it.second); } } return ret; } string countOfAtoms(string s){ map<string, int> m; stack<map<string, int> > st; int i = 0; int n = s.size(); while (i < n) { char c = s[i]; i++; if (c == '(') { st.push(m); m = map<string, int>(); } else if (c == ')') { int val = 0; while (i < n && s[i] >= '0' && s[i] <= '9') { val = val * 10 + (s[i] - '0'); i++; } map<string, int> temp = st.top(); st.pop(); for (auto& it : m) { it.second *= val; temp[it.first] += it.second; } m = temp; } else { string name = ""; int val = 0; name += c; while (i < n && s[i] >= 'a' && s[i] <= 'z') { name += s[i]; i++; } while (i < n && s[i] >= '0' && s[i] <= '9') { val = val * 10 + (s[i] - '0'); i++; } val = val == 0 ? 1 : val; m[name] += val; } } return makeRet(m); } }; main(){ Solution ob; cout << (ob.countOfAtoms("Na2(CO)3")); }
Input
Na2(CO)3
Output
C3Na2O3
- Related Articles
- Calculate the number of moles in 12.044 × 1025 atoms of phosphorus.
- If 32 g of sulphur has x atoms, then the number of atoms in 32 g of oxygen will be :(a) x/2 (b) 2x (c) x (d) 4x
- If 12 gram of carbon has x atoms, then the number of atoms in 12 grams of magnesium will be :(a) x (b) 2x (c) x/2 (d) 1.5x
- In a sample of ethyl ethanoate (CH3COOC2H5), the two oxygen atoms have the same number of electrons but different number of neutrons. Which of the following is the correct reason for it?(a) one of the oxygen atoms has gained electrons(b) one of the oxygen atoms has gained protons(c) the two oxygen atoms are isotopes(d) the two oxygen atoms are isobars
- Find the number of atoms in the following:(i) 0.5 mole of C atom(ii) 2 mole of N atom
- (a) How many atoms are there in exactly 12 g of carbon -12 element? (C = 12 u)(b) What name is given to this number?(c) What name is given to the amount of substance containing this number of atoms?
- Atoms of elements having the same mass number but different atomic number are called ?
- (a) What is the number of valence electrons in the atoms of first element in a period?(b) What is the usual number of valence electrons in the atoms of the last element in a period?
- Raunak took 5 moles of carbon atoms in a container and Krish also took 5 moles of sodium atoms in another container of same weight.(a) Whose container is heavier?(b) Whose container has more number of atoms?
- If one gram of sulphur contains x atoms, calculate the number of atoms in one gram of oxygen element.(Atomic masses : S = 32 u ; O = 16 u)
- Atoms of different elements with the same number of occupied shells are placed in the same _________________________.
- Which of the following statement is true?(a) According to Dalton, atoms are divisible.(b) Atoms do not take part in a chemical reaction(c) The atoms of an element are different from those of other elements(d) Atoms can be broken into molecules.
- How many grams of magnesium will have the same number of atoms as 6 grams of carbon? (Mg = 24 u ; C = 12 u)
- Which of the following has maximum number of atoms?(a) 18g of H2O(b) 18g of O2(c) 18g of CO2(d) 18g of CH4
- What name is given to those atoms which contain the same number of protons and electrons but different number of neutrons?
