You get an algebra expression as a string and a list of variables whose values are known: evalvars[i] equals evalints[i]. The expression uses +, -, *, parentheses, non-negative integers and variable names made of lowercase letters (a name can be several letters long). Every token is separated by one space, and * binds tighter than + and -.
Simplify it: plug in the known values, multiply everything out and merge like terms. Return the result as a list of strings, one per term with a non-zero coefficient. A term is its coefficient, then * and its variables in alphabetical order, like "3*a*b*b"; a coefficient of 1 is still written, and a constant is just its number.
Order the terms by degree (how many variables, counting repeats), highest first. Terms of equal degree are ordered by their variable part as a string. The constant, if any, comes last.
Example 1
Input: expression = "x + 4 - y + 2", evalvars = ["x"], evalints = [3] Output: ["-1*y", "9"] Why: 3 + 4 + 2 = 9, and y is left with coefficient -1
2 more examples and the constraints
Example 2
Input: expression = "(a + k) * a", evalvars = ["k"], evalints = [2] Output: ["1*a*a", "2*a"] Why: (a + 2) * a = a*a + 2*a
Example 3
Input: expression = "b * a * 2 - a * b", evalvars = [], evalints = [] Output: ["1*a*b"] Why: b*a is a*b, so 2*a*b - a*b = a*b
Constraints
- 1 ≤ length of expression ≤ 250
- expression holds lowercase letters, digits,
+ - * ( )and single spaces between tokens - 0 ≤ length of evalvars = length of evalints ≤ 100; each name is 1 to 20 lowercase letters
- −100 ≤ evalints[i] ≤ 100, and every coefficient along the way fits in a 32-bit integer
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Recursive descentO(n · T²)
- Two stacks, one passO(n · T²)
The idea in one sentence
Side by side
| Approach | Time | Relative work for a 250-character expression | Verdict |
|---|---|---|---|
| Recursive descent | O(n · T²) | Passes; mirrors the grammar | |
| Two stacks, one pass | O(n · T²) | Best; flat loop, easy to trace |
n = tokens, T = most terms in any intermediate polynomial.
Edge cases to test
- Everything cancels. "7 - 7" returns an empty list, not ["0"].
- Known value of 0. "q * w + 5" with q = 0 drops the q*w term: ["5"].
- Same variables, other order. b * a and a * b must land on the same key a*b.
- Multi-letter names. "ab * c" and "a * bc" are different terms; order them as strings: a*bc before ab*c.
- Coefficient 1. Write "1*a", never just "a".
Common mistakes
- Leaving the variables in a term unsorted, so a*b and b*a never merge.
- Dropping zero terms during the computation instead of at the end, or forgetting to drop them at all.
- Splitting tokens on spaces only, so brackets stay glued to names like "(a".
How to talk through it
- 1Clarify. Can names be several letters? Is a coefficient of 1 printed? What if everything cancels?
- 2Pick a representation. A map from sorted variable list to coefficient; constants use the empty key.
- 3Define the operations. Add merges maps, multiply crosses every pair of terms, both merge like keys.
- 4Plug in a parser. Recursive descent or two stacks; values are polynomials instead of numbers.
- 5Test out loud. Run a case that cancels to nothing and one with b * a vs a * b.