Problem Statement
You will be given a boolean expression in 2 variables a and b. The expression will resemble a normal arithmetic expression with the following exceptions:
- 1) Multiplication denotes 'and'ing. For example, ab means a AND b.
- 2) Addition denotes 'or'ing. For example, ab+b means (a AND b) OR b. As usual, multiplication has higher precedence than addition.
- 3) If a variable is capitalized, it is negated. For example, Ab means (NOT a) AND b. As seen, negation has higher precedence than multiplication.
<S> := <Term> | <S>+<Term> <Term> := a | b | A | B | <Term><Term>Two boolean expressions have the same value if and only if they agree regardless of the truth values of a and b. You will return the shortest String that adheres to the grammar above, and is equivalent to exp in value. If two Strings have the same length, choose the lexicographically first one. The ordering of characters is +,A,B,a,b (same as in ASCII). As special cases, if the given expression is always true (regardless of the values of a and b) return "True" (quotes for clarity). Similarly, if the given expression is always false return "False".
Definition
- Class:
- ShortBooleanExp
- Method:
- getFirst
- Parameters:
- String
- Returns:
- String
- Method signature:
- String getFirst(String exp)
- (be sure your method is public)
Constraints
- exp will contain between 1 and 50 characters inclusive.
- exp will adhere to the grammar given above.
Examples
"AB+ab+abaB"
Returns: "AB+ab"
"b+AB+bB+B+aabbabab"
Returns: "True"
"bB+Aa+baAB"
Returns: "False"
"ababababababababaaaaaaaaa+bbbbbbbbbbbbbbbb"
Returns: "b"