Statistics

Problem Statement for "Stoich"

Problem Statement

PROBLEM STATEMENT

In the following problem, all quotes and bounding []s are for illustrative
purposes only.
The digit '0' is always spelled as 'zero' in this problem.  If something looks
ambiguous, it is the capital letter 'O'.

Given a chemical equation, balance it so that the quantity of each atom on the
left side of the equation is equal to the quantity of that particular atom on
the right.
For the purposes of this problem, the chemical elements will be limited to 'H'
(hydrogen), 'O' (oxygen), 'N' (nitrogen), 'C' (carbon), and 'S' (sulfur).  

Each molecule will consist of one or more elements each optionally followed by
a single digit 2 or greater specifying a quantity.  For example, "S4" would
represent 4 atoms of sulfur, "H" would represent 1 atom of hydrogen, and
"H2SO4" would represent 2 atoms of hydrogen, 1 atom of sulfur, and 4 atoms of
oxygen.  An element will not be repeated twice within a molecule, so HCOH is
invalid.

The equation will be of the form "[molecule] + [molecule] -> [molecule]"
where [molecule] is a String formatted in the manner described in the previous
paragraph, like "H2SO4" or "H", with exactly one space on both sides of the "+"
and "->" in the exact format shown above. 

Your goal will be to balance the equation by adding a positive integer
coefficient and a space in front of each of the three molecules which
represents the quantity of the molecule as a whole. For example, "2 H2O"
represents 2 molecules of "H2O", or a total of 4 hydrogen atoms and 2 oxygen
atoms. 

All coefficients should be in lowest terms.
Lowest terms means that you should not have any factor common to all three
coefficients.

1,2,4 are in lowest terms.
2,4,8 are not (2 is a common factor between all 3 terms)

If the equation cannot be balanced, return "NO SOLUTION".
If there are multiple lowest term solutions, return "MULTIPLE SOLUTIONS"

Example: 

equation = "SO2 + O2 -> SO3"

the first molecule has 1 atom of sulfur and 2 of oxygen
the second molecule has 2 atoms of oxygen
the last has 1 atom of sulfur and 3 of oxygen

There is 1 atom of sulfur and 4 atoms of oxygen on the left side of the equation.
There is 1 atom of sulfur and 3 atoms of oxygen on the right side of the
equation.

We can balance the equation as follows:
"2 SO2 + 1 O2 -> 2 SO3"

Now there are 2 atoms of sulfur and 6 atoms of oxygen on the left.
There are also 2 atoms of sulfur and 6 atoms of oxygen on the right, so the
equation is balanced.

Note that "4 SO2 + 2 O2 -> 4 SO3" also works, but it isn't a valid solution
because it isn't in lowest terms. Always express your answer in lowest terms by
dividing out the common factors of the coefficients you find.

Your method would return "2 SO2 + 1 O2 -> 2 SO3"

DEFINITION

Class: Stoich
Method: balanceEq
Parameters: String
Returns: String
Method signature (be sure your method is public): String balanceEq(String
equation);

NOTES

- if the equation cannot be balanced, return "NO SOLUTION".
- if there is more than one lowest term solution, return "MULTIPLE SOLUTIONS".
- molecules may not have a zero coefficient. You must balance the equation
using only positive integral coefficients.
- each molecule in your return value should look exactly like its matching
molecule in the input equation.  Do not reorder the atoms.  The only difference
should be adding a coefficient and single space before each molecule.
- there will be no leading digits in the input equation.
- formatting is important.  See the examples, or read the problem description
above if you have questions.

TopCoder will ensure the validity of the inputs.  Inputs are valid if all of
the following criteria are met:
- equation will be between 10 and 37 characters, inclusive.
- equation will be in the form "[molecule] + [molecule] -> [molecule]" exactly,
with [molecule] being defined below.
- each [molecule] will be a list of uppercase chemical letters ('H', 'O', 'N',
'C', and 'S'), each letter optionally followed by a single digit, '2'-'9',
inclusive.
- a chemical letter will not appear twice within the same [molecule].
- each [molecule] will contain at least one of the chemical letters.
- there will be no extra trailing, leading, or internal spaces other than the
single space on each side of the "+" and on each side of the "->".

EXAMPLES

(1)
equation = "N2 + H2 -> NH3"

the method would return "1 N2 + 3 H2 -> 2 NH3"

(2)
equation = "O2 + O4 -> O6"

This can be solved as several different combinations in lowest terms; for
example, 
"1 O2 + 1 O4 -> 1 O6" (already solved)
"4 O2 + 1 O4 -> 2 O6" (4, 1, and 2 are in lowest terms)

the method would return "MULTIPLE SOLUTIONS"

(3)
equation = "NO2 + CH -> SON"

There is no way to balance this equation, since there is no 'S' on the left
side, and no 'C' or 'H' on the right.

the method would return "NO SOLUTION"

(4)
equation = "C8H9N4O2 + H -> C4H6N2O"

the method would return "1 C8H9N4O2 + 3 H -> 2 C4H6N2O"

(5)
equation = "CN4 + N3 -> CN"

Here, 1 -1 1 would work, but you cannot have a zero or negative amount of a
molecule.

the method would return "NO SOLUTION"

Definition

Class:
Stoich
Method:
balanceEq
Parameters:
String
Returns:
String
Method signature:
String balanceEq(String param0)
(be sure your method is public)

Constraints

    Examples


      This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
      This problem was used for: