TopcoderArchive
Archive
Algorithm problem

ArithmeticEquation

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given a String s that consists of only digits. You would like to transform it into a correct arithmetic equality of the form "X+Y=Z", "X-Y=Z" or "X*Y=Z" (quotes for clarity), where X, Y and Z are integers with no extra leading zeroes. To do this, you are allowed to replace one character in s with '+', '-' or '*', and another with '=' (the '=' character must appear later than the operator).

Return the equality into which s can be transformed. If there are multiple possibilities, return the one that comes first lexicographically. If there is no solution, return "NO SOLUTION" (quotes for clarity).

Definition

Class:
ArithmeticEquation
Method:
createEquality
Parameters:
String
Returns:
String
Method signature:
String createEquality(String s)
(be sure your method is public)

Notes

  • The lexicographical ordering for characters in this problem is: '*', '+', '-', '0', ..., '9', '='.

Constraints

  • s will contain only digits ('0'-'9').
  • s will contain between 5 and 10 characters, inclusive.

Examples

  1. "22224"
    Returns: "2*2=4"
    There are two solutions in this case: "2+2=4" and "2*2=4". The one with multiplication comes first lexicographically.
  2. "22223"
    Returns: "NO SOLUTION"
  3. "22220"
    Returns: "2-2=0"
    Extra leading zeroes are forbidden, but 0 by itself is allowed.
  4. "1111111122"
    Returns: "11+111=122"
  5. "000000"
    Returns: "NO SOLUTION"
Back to all problems