java.lang.Object
g2001_2100.s2019_the_score_of_students_solving_math_expression.Solution

public class Solution extends Object
2019 - The Score of Students Solving Math Expression.<p>Hard</p> <p>You are given a string <code>s</code> that contains digits <code>0-9</code>, addition symbols <code>'+'</code>, and multiplication symbols <code>'*'</code> <strong>only</strong> , representing a <strong>valid</strong> math expression of <strong>single digit numbers</strong> (e.g., <code>3+5*2</code>). This expression was given to <code>n</code> elementary school students. The students were instructed to get the answer of the expression by following this <strong>order of operations</strong>:</p> <ol> <li>Compute <strong>multiplication</strong> , reading from <strong>left to right</strong>; Then,</li> <li>Compute <strong>addition</strong> , reading from <strong>left to right</strong>.</li> </ol> <p>You are given an integer array <code>answers</code> of length <code>n</code>, which are the submitted answers of the students in no particular order. You are asked to grade the <code>answers</code>, by following these <strong>rules</strong>:</p> <ul> <li>If an answer <strong>equals</strong> the correct answer of the expression, this student will be rewarded <code>5</code> points;</li> <li>Otherwise, if the answer <strong>could be interpreted</strong> as if the student applied the operators <strong>in the wrong order</strong> but had <strong>correct arithmetic</strong> , this student will be rewarded <code>2</code> points;</li> <li>Otherwise, this student will be rewarded <code>0</code> points.</li> </ul> <p>Return <em>the sum of the points of the students</em>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/09/17/student_solving_math.png" alt="" /></p> <p><strong>Input:</strong> s = &ldquo;7+3*1*2&rdquo;, answers = [20,13,42]</p> <p><strong>Output:</strong> 7</p> <p><strong>Explanation:</strong> As illustrated above, the correct answer of the expression is 13, therefore one student is rewarded 5 points: [20, <strong>13</strong> ,42]</p> <p>A student might have applied the operators in this wrong order: ((7+3)*1)*2 = 20. Therefore one student is rewarded 2 points: [<strong>20</strong> ,13,42]</p> <p>The points for the students are: [2,5,0]. The sum of the points is 2+5+0=7.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;3+5*2&rdquo;, answers = [13,0,10,13,13,16,16]</p> <p><strong>Output:</strong> 19</p> <p><strong>Explanation:</strong> The correct answer of the expression is 13, therefore three students are rewarded 5 points each: [<strong>13</strong> ,0,10, <strong>13</strong> , <strong>13</strong> ,16,16]</p> <p>A student might have applied the operators in this wrong order: ((3+5)*2 = 16. Therefore two students are rewarded 2 points: [13,0,10,13,13, <strong>16</strong> , <strong>16</strong> ]</p> <p>The points for the students are: [5,0,0,5,5,2,2]. The sum of the points is 5+0+0+5+5+2+2=19.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;6+0*1&rdquo;, answers = [12,9,6,4,8,6]</p> <p><strong>Output:</strong> 10</p> <p><strong>Explanation:</strong> The correct answer of the expression is 6.</p> <p>If a student had incorrectly done (6+0)*1, the answer would also be 6.</p> <p>By the rules of grading, the students will still be rewarded 5 points (as they got the correct answer), not 2 points.</p> <p>The points for the students are: [0,0,5,0,0,5]. The sum of the points is 10.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 <= s.length <= 31</code></li> <li><code>s</code> represents a valid expression that contains only digits <code>0-9</code>, <code>'+'</code>, and <code>'*'</code> only.</li> <li>All the integer operands in the expression are in the <strong>inclusive</strong> range <code>[0, 9]</code>.</li> <li><code>1 <=</code> The count of all operators (<code>'+'</code> and <code>'*'</code>) in the math expression <code><= 15</code></li> <li>Test data are generated such that the correct answer of the expression is in the range of <code>[0, 1000]</code>.</li> <li><code>n == answers.length</code></li> <li><code>1 <= n <= 10<sup>4</sup></code></li> <li><code>0 <= answers[i] <= 1000</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • scoreOfStudents

      public int scoreOfStudents(String s, int[] answers)