Calculating dehydration synthesis results Disclaimer: This challenge is extremely scientifically inaccurate.
;)

Given an input of a list of molecules, output what the result would be if they
were to all combine via dehydration synthesis, if possible.

For the purposes of this challenge, although this description is inaccurate, a
dehydration reaction
between two molecules is performed as follows:


Remove OH (one oxygen and one hydrogen atom) from either of the two
molecules.
Remove H (a single hydrogen atom) from the other one.
Combine the two molecules into one.


For instance, combining two
C6H12O6 molecules via dehydration
synthesis would result in
C12H22O11. Note that there are
two less Hs and one less O in the product than in the reactants. (These
missing parts would form H2O, hence "dehydration," but
that's irrelevant to this challenge.)

An example of molecules that cannot be combined via dehydration synthesis is
C6H12O6 +
CO2. The required amount of hydrogen and oxygen is
present, but neither OH nor H can be taken from the
CO2.

The format of the input for each molecule is a list of atoms concatenated. An
atom is always a single capital letter, possibly followed by a single lowercase
letter (you don't have to handle atoms like Uuo). Atoms may also have
integers ≥ 2 after them, representing how many of them there are; if this
number is not present, it is assumed to be 1. Atoms will never be repeated
within the same molecule.

Input may be provided as either an array/list/etc. of strings, or a single
string separated by any non-alphanumeric character. Output must be a single
string, but the output for "invalid input" (i.e. a list of molecules that
cannot be combined via dehydration synthesis) may be any string containing at
least one non-alphanumeric character (to distinguish from valid output). The
order of the atoms in the output does not matter.

Since this is code-golf, the shortest code in bytes will win.

Test cases (invalid output is represented here by an asterisk *):

In                                                Out
------------------------------------------------------------------
C6H12O6 C6H12O6 C6H12O6 C6H12O6 C6H12O6 C6H12O6 | C36H62O31
CHO5 CH2 CH2 CH2 CH2 CH2                        | C6H
CHO4 CH2 CH2 CH2 CH2 CH2                        | *
H H H CH3O3                                     | C
H H H CH4O2                                     | *
C6H12O6 ABCDELMNOP C6H12O6                      | *
HO HO HO H3                                     | &lt;empty string&gt;
HgO4 C6H12O6                                    | *
HgHO4 C6H12O6                                   | HgH11O9C6
PdZnXeRaHO KrArKRbFH                            | PdZnXeRaKrArKRbF
Python (337 336 chars)
import re
i=input().split()
a,d,o,e=[],{},&quot;&quot;,len(i)-1
for b in i:a.append(dict(re.findall(r&quot;([A-Z][a-z]*)(\d*)&quot;,b)))
a=[{b:int(c[b])if c[b]else 1for b in c}for c in a]
for b in a:
 for c in b:d[c]=c in d and d[c]+b[c]or b[c] 
 if&quot;H&quot;not in b:d[&quot;-&quot;]=0
d[&quot;H&quot;]-=2*e;d[&quot;O&quot;]-=e
for b in d:o+=b+(str(d[b])if d[b]-1else&quot;&quot;)if d[b]else&quot;&quot;
print(o)

Here is an example of how to use this program:
sh3.14$ python dehydrate.py &lt;-Run the program
C6H12O6 HgHO4               &lt;-Input
O9H11C6Hg                   &lt;-Output

Try it!