The Score of Students Solving Math Expression

You are given a string s that contains digits 0-9, addition symbols '+', and multiplication symbols '*' only, representing a valid math expression of single digit numbers. This expression was given to n elementary school students. The students were instructed to get the answer of the expression by following this order of operations:

  • Compute multiplication, reading from left to right; then,
  • Compute addition, reading from left to right.

You are given an integer array answers of length n, which are the submitted answers of the students in no particular order. You are asked to grade the answers by following these rules:

  • If an answer equals the correct answer of the expression, this student will be rewarded 5 points.
  • Otherwise, if the answer could be interpreted as if the student applied the operators in the wrong order but had correct arithmetic, this student will be rewarded 2 points.
  • Otherwise, this student will be rewarded 0 points.

Return the sum of the points of the students.

Example 1
Inputs = "7+3*1*2", answers = [20,13,42]
Output7
The correct answer is 13 for 5 points, and 20 can be produced by applying operators in a wrong order with correct arithmetic for 2 points, so the total is 7.
Example 2
Inputs = "3+5*2", answers = [13,0,10,13,13,16,16]
Output19
Three answers equal the correct answer 13 for 15 points total, and two answers equal 16 from a possible wrong operator order for 4 more points, so the total is 19.

Constraints

  • 3 <= s.length <= 31
  • s represents a valid expression that contains only digits 0-9, '+', and '*' only.
  • All the integer operands in the expression are in the inclusive range [0, 9].
  • 1 <= The count of all operators ('+' and '*') in the math expression <= 15
  • Test data are generated such that the correct answer of the expression is in the range of [0, 1000].
  • Test data are generated such that value never exceeds 10^9 in intermediate steps of multiplication.
  • n == answers.length
  • 1 <= n <= 10^4
  • 0 <= answers[i] <= 1000

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate