Find the Oxidation States Challenge

Find the oxidation states of each of the atoms in a given molecule. These can be output as a list or otherwise.

Rules

The total oxidation state of a molecule will always be zero. The total oxidation state is the sum of the oxidation states of each of the individual atoms in the molecule.

The following atoms have constant oxidation states:


Hydrogen, H, always has state +1
Oxygen, O, always has state -2
All Group 1 elements have a state +1
All Group 2 elements have a state +2
Fluorine, F, always has state -1
Chlorine, Cl, always has state -1


In monatomic molecules (such as H2 or S8), all of the atoms always have a state 0. I.e. (H2 is 0, 0 and S8 is 0, 0, 0, 0, 0, 0, 0, 0)

The Group 1 elements are: Li, Na, K, Rb, Cs, Fr. The Group 2 elements are: Be, Mg, Ca, Sr, Ba, Ra.

You will be able to work out the states of every atom in the molecule given. There will be no ambiguous inputs (i.e. you won't be given H2O2 or P4Br6).

You should output the oxidation states of the individual atoms, not the total state.

If there is an element which is not listed in the list above in the molecule, you need to work out its oxidation state yourself since the sum of the oxidation states of all the atoms adds up to zero.

The molecules do not have to exist in real life.

You will never get a single atom such as Ti or F.

Built-in functions which access data about oxidation states are disallowed.

Examples

Input &gt; Output
H2O &gt; +1, +1, -2
CO2 &gt; +4, -2, -2
CH4 &gt; -4, +1, +1, +1, +1
H2SO4 &gt; +1, +1, +6, -2, -2, -2, -2
NaHCO3 &gt; +1, +1, +4, -2, -2, -2
XeF8 &gt; +8, -1, -1, -1, -1, -1, -1, -1, -1
TiCl2 &gt; +2, -1, -1
P4 &gt; 0, 0, 0, 0


Winning

The shortest code in bytes wins.
Ruby, 288 255 bytes

-&gt;m{h={[?O]=&gt;-2,%w[F Cl]=&gt;-1,%w[H Li Na K Rb Cs Fr]=&gt;1,%w[Be Mg Ca Sr Ba Ra]=&gt;2};a=m.scan /([A-Z][a-z]*\d*)/;r=a.flat_map{|(b)|b=~/(\D+)(\d*)/;[a.size==1?0:h[h.keys.find{|v|v.include?$1}]]*($2=='' ? 1:$2.to_i)};r.map{|j|j||-((r-[p]).inject:+)/r.count(p)}}

Try it online!

Saved 1 byte thanks to Jenkar
Saved 32 more bytes by inlining definitions and removing parentheses

Ungolfed
oxidation_state = -&gt; molecule {
    h = {
        %w[H] =&gt; 1,
        %w[O] =&gt; -2,
        %w[F] =&gt; -1,
        %w[Cl] =&gt; -1,
        %w[Li Na K Rb Cs Fr] =&gt; 1,
        %w[Be Mg Ca Sr Ba Ra] =&gt; 2,
    }
    find_oxidation = -&gt; a {
        k = h.keys.find { |as| as.include?(a) }
        h[k]
    }
    atoms = molecule.scan(/([A-Z][a-z]*\d*)/)
    r = atoms.flat_map { |(a)|
        a.match(/([^\d]+)(\d*)/)
        [atoms.length == 1 ? 0 : find_oxidation[$1]] * ($2=='' ? 1 : $2.to_i)
    }
    r.map { |j|
        sum = r.compact.inject(:+)
        !j ? -sum / r.count(nil) : j
    }
}