Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    770 - Basic Calculator IV\.

    Hard

    Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {"e": 1} (given in terms of evalvars = ["e"] and evalints = [1]), return a list of tokens representing the simplified expression, such as ["-1*a","14"]

    • An expression alternates chunks and symbols, with a space separating each chunk and symbol.

    • A chunk is either an expression in parentheses, a variable, or a non-negative integer.

    • A variable is a string of lowercase letters (not including digits.) Note that variables can be multiple letters, and note that variables never have a leading coefficient or unary operator like "2x" or "-x".

    Expressions are evaluated in the usual order: brackets first, then multiplication, then addition and subtraction.

    • For example, expression = "1 + 2 * 3" has an answer of ["7"].

    The format of the output is as follows:

    • For each term of free variables with a non-zero coefficient, we write the free variables within a term in sorted order lexicographically.

    • Terms have degrees equal to the number of free variables being multiplied, counting multiplicity. We write the largest degree terms of our answer first, breaking ties by lexicographic order ignoring the leading coefficient of the term.

    • The leading coefficient of the term is placed directly to the left with an asterisk separating it from the variables (if they exist.) A leading coefficient of 1 is still printed.

    • An example of a well-formatted answer is ["-2*a*a*a", "3*a*a*b", "3*b*b", "4*a", "5*c", "-6"].

    • Terms (including constant terms) with coefficient 0 are not included.

    Note: You may assume that the given expression is always valid. All intermediate results will be in the range of <code>-2<sup>31</sup>, 2<sup>31</sup> - 1</code>.

    Example 1:

    Input: expression = "e + 8 - a + 5", evalvars = "e", evalints = 1

    Output: "-1\*a","14"

    Example 2:

    Input: expression = "e - 8 + temperature - pressure", evalvars = "e", "temperature", evalints = 1, 12

    Output: "-1\*pressure","5"

    Example 3:

    Input: expression = "(e + 8) \* (e - 8)", evalvars = [], evalints = []

    Output: "1\*e\*e","-64"

    Constraints:

    • 1 &lt;= expression.length &lt;= 250

    • expression consists of lowercase English letters, digits, '+', '-', '*', '(', ')', ' '.

    • expression does not contain any leading or trailing spaces.

    • All the tokens in expression are separated by a single space.

    • 0 &lt;= evalvars.length &lt;= 100

    • 1 &lt;= evalvars[i].length &lt;= 20

    • evalvars[i] consists of lowercase English letters.

    • evalints.length == evalvars.length

    • -100 &lt;= evalints[i] &lt;= 100

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final List<String> basicCalculatorIV(String expression, Array<String> evalvarS, IntArray evalintS)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait