The Score of Students Solving Math Expression - Problem

You're a teacher grading math homework! 📝 You've given your elementary school students a simple arithmetic expression containing only single digits (0-9), addition (+), and multiplication (*) symbols.

Here's the catch: you taught them a specific order of operations:

  1. First: Compute all multiplications from left to right
  2. Then: Compute all additions from left to right

For example, with expression "3+5*2":

  • Correct answer: 3 + (5*2) = 3 + 10 = 13
  • Wrong order: (3+5) * 2 = 8 * 2 = 16

Now you need to grade the students' answers based on these rules:

  • 5 points: Correct answer using proper order of operations
  • 2 points: Wrong answer BUT could be obtained by applying operations in some different (but mathematically valid) order
  • 0 points: Completely wrong answer that can't be explained by any order of operations

Goal: Return the total points earned by all students.

Input & Output

example_1.py — Basic Expression
$ Input: s = "7+3*1*2", answers = [20, 13, 42]
Output: 7
💡 Note: Correct answer: 7+(3*1*2) = 7+6 = 13. Student with answer 13 gets 5 points. Answer 20 could come from (7+3)*1*2 = 20, so 2 points. Answer 42 is impossible with any valid parenthesization, so 0 points. Total: 5+2+0 = 7
example_2.py — Simple Addition
$ Input: s = "3+5*2", answers = [13, 0, 10, 13, 13, 16, 16]
Output: 19
💡 Note: Correct answer: 3+(5*2) = 13. Three students got 13 (5×3=15 points). Two students got 16 from (3+5)*2 (2×2=4 points). Answers 0 and 10 are impossible (0×2=0 points). Total: 15+4+0 = 19
example_3.py — Single Number
$ Input: s = "6+0*1", answers = [6, 1, 0]
Output: 7
💡 Note: Correct answer: 6+(0*1) = 6+0 = 6. Student with answer 6 gets 5 points. Answer 0 could come from (6+0)*1 = 0, so 2 points. Answer 1 is impossible, so 0 points. Total: 5+2+0 = 7

Constraints

  • 3 ≤ s.length ≤ 31
  • s consists of digits and operators '+' and '*' only
  • All the integer operands in the expression are in the range [0, 9]
  • 1 ≤ answers.length ≤ 104
  • 0 ≤ answers[i] ≤ 1000
  • The expression is guaranteed to be valid (alternating numbers and operators)

Visualization

Tap to expand
Math Expression Scoring - DP Approach INPUT Expression: "7+3*1*2" Tokens: digits + operators 7 + 3 * 1 * 2 Student Answers: 20 13 42 Scoring Rules: 5 pts: Correct answer 2 pts: Valid alt order 0 pts: Invalid answer Correct: * before + ALGORITHM STEPS 1 Compute Correct Answer 7+(3*1*2) = 7+6 = 13 2 DP: All Possible Results dp[i][j] = set of results dp[0][0]={7} dp[1][1]={3} dp[2][2]={1} dp[3][3]={2} Merge subranges: dp[0][3] = {13, 16, 20, 80} (7+3)*(1*2)=20, 7+(3*1)*2=... 3 Build Valid Set All orderings: {13,16,20,80} 4 Score Each Answer 20: in set, not 13 --> 2 pts 13: equals correct --> 5 pts 42: not in set --> 0 pts Total: 2 + 5 + 0 = 7 FINAL RESULT All Valid Results (DP Set) {13, 16, 20, 80} Correct Answer = 13 Score Breakdown: Answer 20: In set +2 Answer 13: Correct! +5 Answer 42: Invalid +0 OUTPUT 7 [OK] Total Points Key Insight: Use Interval DP to compute all possible results from any order of operations. dp[i][j] stores all values obtainable from subexpression [i..j]. Merge ranges by trying each split point k and combining results with each operator. Time: O(n^3 * |results|^2), Space: O(n^2 * |results|). Results bounded to 1000. TutorialsPoint - The Score of Students Solving Math Expression | DP Approach
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.4K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen