TopcoderArchive
Archive
Algorithm problem

VariableAddition

Problem statement, definition, constraints, and public examples.

Problem Statement

You will be given a String, eq, representing some simple addition. It will be formatted as a sequence of terms separated by single plus signs. Each term will be either an integer between 0 and 1000, inclusive, or a sequence of lowercase letters, representing a variable. The values of all the variables in eq will be given in a String[], vars, each element of which will be formatted as "VARIABLE VALUE". You should return the sum represented by eq.

Definition

Class:
VariableAddition
Method:
add
Parameters:
String, String[]
Returns:
int
Method signature:
int add(String eq, String[] vars)
(be sure your method is public)

Constraints

  • eq will be formatted as described in the problem.
  • eq will contain at least one plus sign.
  • eq will contain between 3 and 50 characters, inclusive.
  • Each element of vars will be formatted as "VARIABLE VALUE", where VARIABLE is a sequence of lowercase letters and VALUE is an integer between 0 and 1000, inclusive.
  • None of the integers in the input will have extra leading zeros.
  • Each variable in eq will be present in vars.
  • Each variable in vars will be present in eq, and no variable will occur more than once in vars.
  • Each element of vars will contain at most 50 characters.

Examples

  1. "1+2+3"
    {}
    Returns: 6
  2. "a+aa+aaa"
    {"a 1","aa 2","aaa 1"}
    Returns: 4
  3. "1+3+a+xyz+33+num+num"
    {"a 999","xyz 0","num 11"}
    Returns: 1058
Back to all problems