Problem Statement
You are given the String comparisons with n characters. Each character in comparisons is one of '<', '>', '=', and '!'. These represent the relations "is less than", "is greater than", "equals", and "does not equal", respectively.
A positive integer X with n+1 digits (and with no leading zeros) is said to match the given comparisons if all the relations are true for pairs of consecutive digits of X, in order. For example, suppose comparisons is "<<=!". One integer that matches these comparisons is 14770. This is because 1<4, 4<7, 7=7, and 7!=0.
If no (n+1)-digit integer matches all the given comparisons, return an empty String. Otherwise return a String with the digits of the smallest such number.
Definition
- Class:
- ReconstructNumber
- Method:
- smallest
- Parameters:
- String
- Returns:
- String
- Method signature:
- String smallest(String comparisons)
- (be sure your method is public)
Constraints
- comparisons will have between 0 and 2000 characters, inclusive.
- Each character in comparisons will be one of "<>=!".
Examples
">=!<"
Returns: "10012"
We have 1 > 0, 0 = 0, 0 != 1, and 1 < 2. There are other numbers that match these comparisons, but this one is the smallest of them all."====!===="
Returns: "1111100000"
""
Returns: "1"
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
Returns: ""
"><<>><=<=>=>"
Returns: "1012101122110"