Tap here to switch tabs
Problem
Submissions

Problem: Basic Calculator IV

med
30 min
Try to solve the Basic Calculator IV problem.

Statement

Given an algebraic expression represented by the string expression, along with two arrays evalvars and evalints representing known variable assignments where evalvars[i] is assigned the integer value evalints[i], evaluate and simplify the expression to a canonical polynomial representation.

The canonical representation of a polynomial is a list of strings representing non zero terms sorted according to the following rules:

  • Terms are ordered in descending order by their degree, where the degree of a term is the total number of variable factors it contains.

  • Terms with the same degree are ordered in lexicographical order based on their variable names.

  • Each term with degree greater than 00 is formatted as its integer coefficient followed by its variable factors separated by '*', with variables sorted in lexicographical order (for example, "2*a*b*c" or "-1*a*a").

  • Terms with degree 00 (constant terms) are formatted solely as the integer string (for example, "14" or "-64").

  • Terms with a coefficient of 00 are omitted. If the simplified polynomial evaluates entirely to 00, return an empty list.

The expressions follow standard operator precedence, where multiplication takes precedence over addition and subtraction, and subexpressions enclosed in parentheses are evaluated first.

Constraints:

  • 1≤1 \leq expression.length ≤250\leq 250

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

  • expression does not contain any leading or trailing spaces.

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

  • 0≤0 \leq evalvars.length ≤100\leq 100

  • 1≤1 \leq evalvars[i].length ≤20\leq 20

  • evalvars[i] consists of lowercase English letters.

  • evalints.length ==== evalvars.length

  • −100≤-100 \leq evalints[i] ≤100\leq 100

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Basic Calculator IV

med
30 min
Try to solve the Basic Calculator IV problem.

Statement

Given an algebraic expression represented by the string expression, along with two arrays evalvars and evalints representing known variable assignments where evalvars[i] is assigned the integer value evalints[i], evaluate and simplify the expression to a canonical polynomial representation.

The canonical representation of a polynomial is a list of strings representing non zero terms sorted according to the following rules:

  • Terms are ordered in descending order by their degree, where the degree of a term is the total number of variable factors it contains.

  • Terms with the same degree are ordered in lexicographical order based on their variable names.

  • Each term with degree greater than 00 is formatted as its integer coefficient followed by its variable factors separated by '*', with variables sorted in lexicographical order (for example, "2*a*b*c" or "-1*a*a").

  • Terms with degree 00 (constant terms) are formatted solely as the integer string (for example, "14" or "-64").

  • Terms with a coefficient of 00 are omitted. If the simplified polynomial evaluates entirely to 00, return an empty list.

The expressions follow standard operator precedence, where multiplication takes precedence over addition and subtraction, and subexpressions enclosed in parentheses are evaluated first.

Constraints:

  • 1≤1 \leq expression.length ≤250\leq 250

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

  • expression does not contain any leading or trailing spaces.

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

  • 0≤0 \leq evalvars.length ≤100\leq 100

  • 1≤1 \leq evalvars[i].length ≤20\leq 20

  • evalvars[i] consists of lowercase English letters.

  • evalints.length ==== evalvars.length

  • −100≤-100 \leq evalints[i] ≤100\leq 100