TopCoder problem "VariableSolve" used in TCO05 Round 1 (Division I Level Three)



Problem Statement

    You will be given an equation of the form <side>=<side> where <side> is a list of <term>s separated by <op>s. Each <op> is either + or -, and each <term> is a positive length string of uppercase letters. Each letter denotes a variable taking on real values. No letter will occur in a <term> more than twice.



The <term>s are products of variables. As you would expect, the + and - operators take lower precedence than the multiplications within the <term>s. For example,
	ABC+ADF=PQ-PQ
is a properly formatted equation. Sometimes, by fixing a single variable at a particular value, we can force the entire equation to always be true. Return a String[] containing all such variables that satisfy this requirement. If a variable can be set to n distinct values all of which satisfy this requirement, then that variable should occur n times in the return. If a variable can be set to an infinite number of distinct values that satisfy this requirement, it should not be returned. The return should be sorted in alphabetical order.
 

Definition

    
Class:VariableSolve
Method:getSolutions
Parameters:String
Returns:String[]
Method signature:String[] getSolutions(String equation)
(be sure your method is public)
    
 

Constraints

-equation will contain between 3 and 50 characters inclusive.
-equation will be formatted as described in the problem statement.
 

Examples

0)
    
"P=NP"
Returns: {"N", "P" }
Simpler than it looks: let N = 1 or P = 0.
1)
    
"RED+BLUE=PURPLE"
Returns: {"E" }
Only E = 0 guarantees the equation holds.
2)
    
"ABCD+ABCD-ABCD=ABCD"
Returns: { }
Each variable has an infinite number of values causing the equation to hold.
3)
    
"BAA+BA+B=P-P"
Returns: {"B" }
We cannot consider the complex solutions for A.
4)
    
"ABB-AB-AB+A=BCB-C"
Returns: {"B" }

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=4715

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=8019&pm=4715

Writer:

AdminBrett

Testers:

PabloGilberto , lbackstrom , vorthys , Olexiy

Problem categories:

Math