Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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 (e.g., 3+5*2). 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:
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:
5 points;2 points;0 points.Return the sum of the points of the students.
Example 1:
Input: s = "7+3*1*2", answers = [20,13,42] Output: 7 Explanation: As illustrated above, the correct answer of the expression is 13, therefore one student is rewarded 5 points: [20,13,42] A student might have applied the operators in this wrong order: ((7+3)*1)*2 = 20. Therefore one student is rewarded 2 points: [20,13,42] The points for the students are: [2,5,0]. The sum of the points is 2+5+0=7.
Example 2:
Input: s = "3+5*2", answers = [13,0,10,13,13,16,16] Output: 19 Explanation: The correct answer of the expression is 13, therefore three students are rewarded 5 points each: [13,0,10,13,13,16,16] 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,16,16] 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.
Example 3:
Input: s = "6+0*1", answers = [12,9,6,4,8,6] Output: 10 Explanation: The correct answer of the expression is 6. If a student had incorrectly done (6+0)*1, the answer would also be 6. By the rules of grading, the students will still be rewarded 5 points (as they got the correct answer), not 2 points. The points for the students are: [0,0,5,0,0,5]. The sum of the points is 10.
Constraints:
3 <= s.length <= 31s represents a valid expression that contains only digits 0-9, '+', and '*' only.[0, 9].1 <= The count of all operators ('+' and '*') in the math expression <= 15[0, 1000].n == answers.length1 <= n <= 1040 <= answers[i] <= 1000Problem summary: 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 (e.g., 3+5*2). 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
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math · Dynamic Programming · Stack
"7+3*1*2" [20,13,42]
"3+5*2" [13,0,10,13,13,16,16]
"6+0*1" [12,9,6,4,8,6]
basic-calculator)different-ways-to-add-parentheses)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2019: The Score of Students Solving Math Expression
class Solution {
public int scoreOfStudents(String s, int[] answers) {
int n = s.length();
int x = cal(s);
int m = (n + 1) >> 1;
Set<Integer>[][] f = new Set[m][m];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m; ++j) {
f[i][j] = new HashSet<>();
}
f[i][i].add(s.charAt(i << 1) - '0');
}
for (int i = m - 1; i >= 0; --i) {
for (int j = i; j < m; ++j) {
for (int k = i; k < j; ++k) {
for (int l : f[i][k]) {
for (int r : f[k + 1][j]) {
char op = s.charAt(k << 1 | 1);
if (op == '+' && l + r <= 1000) {
f[i][j].add(l + r);
} else if (op == '*' && l * r <= 1000) {
f[i][j].add(l * r);
}
}
}
}
}
}
int[] cnt = new int[1001];
for (int ans : answers) {
++cnt[ans];
}
int ans = 5 * cnt[x];
for (int i = 0; i <= 1000; ++i) {
if (i != x && f[0][m - 1].contains(i)) {
ans += 2 * cnt[i];
}
}
return ans;
}
private int cal(String s) {
int res = 0, pre = s.charAt(0) - '0';
for (int i = 1; i < s.length(); i += 2) {
char op = s.charAt(i);
int cur = s.charAt(i + 1) - '0';
if (op == '*') {
pre *= cur;
} else {
res += pre;
pre = cur;
}
}
res += pre;
return res;
}
}
// Accepted solution for LeetCode #2019: The Score of Students Solving Math Expression
func scoreOfStudents(s string, answers []int) int {
n := len(s)
x := cal(s)
m := (n + 1) >> 1
f := make([][]map[int]bool, m)
for i := range f {
f[i] = make([]map[int]bool, m)
for j := range f[i] {
f[i][j] = make(map[int]bool)
}
f[i][i][int(s[i<<1]-'0')] = true
}
for i := m - 1; i >= 0; i-- {
for j := i; j < m; j++ {
for k := i; k < j; k++ {
for l := range f[i][k] {
for r := range f[k+1][j] {
op := s[k<<1|1]
if op == '+' && l+r <= 1000 {
f[i][j][l+r] = true
} else if op == '*' && l*r <= 1000 {
f[i][j][l*r] = true
}
}
}
}
}
}
cnt := [1001]int{}
for _, v := range answers {
cnt[v]++
}
ans := cnt[x] * 5
for k, v := range cnt {
if k != x && f[0][m-1][k] {
ans += v << 1
}
}
return ans
}
func cal(s string) int {
res, pre := 0, int(s[0]-'0')
for i := 1; i < len(s); i += 2 {
cur := int(s[i+1] - '0')
if s[i] == '+' {
res += pre
pre = cur
} else {
pre *= cur
}
}
res += pre
return res
}
# Accepted solution for LeetCode #2019: The Score of Students Solving Math Expression
class Solution:
def scoreOfStudents(self, s: str, answers: List[int]) -> int:
def cal(s: str) -> int:
res, pre = 0, int(s[0])
for i in range(1, n, 2):
if s[i] == "*":
pre *= int(s[i + 1])
else:
res += pre
pre = int(s[i + 1])
res += pre
return res
n = len(s)
x = cal(s)
m = (n + 1) >> 1
f = [[set() for _ in range(m)] for _ in range(m)]
for i in range(m):
f[i][i] = {int(s[i << 1])}
for i in range(m - 1, -1, -1):
for j in range(i, m):
for k in range(i, j):
for l in f[i][k]:
for r in f[k + 1][j]:
if s[k << 1 | 1] == "+" and l + r <= 1000:
f[i][j].add(l + r)
elif s[k << 1 | 1] == "*" and l * r <= 1000:
f[i][j].add(l * r)
cnt = Counter(answers)
ans = cnt[x] * 5
for k, v in cnt.items():
if k != x and k in f[0][m - 1]:
ans += v << 1
return ans
// Accepted solution for LeetCode #2019: The Score of Students Solving Math Expression
/**
* [2019] 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 (e.g., 3+5*2). 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:
* <ol>
* Compute multiplication, reading from left to right; Then,
* Compute addition, reading from left to right.
* </ol>
* 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:
* <img alt="" src="https://assets.leetcode.com/uploads/2021/09/17/student_solving_math.png" style="width: 678px; height: 109px;" />
* Input: s = "7+3*1*2", answers = [20,13,42]
* Output: 7
* Explanation: As illustrated above, the correct answer of the expression is 13, therefore one student is rewarded 5 points: [20,<u>13</u>,42]
* A student might have applied the operators in this wrong order: ((7+3)*1)*2 = 20. Therefore one student is rewarded 2 points: [<u>20</u>,13,42]
* The points for the students are: [2,5,0]. The sum of the points is 2+5+0=7.
*
* Example 2:
*
* Input: s = "3+5*2", answers = [13,0,10,13,13,16,16]
* Output: 19
* Explanation: The correct answer of the expression is 13, therefore three students are rewarded 5 points each: [<u>13</u>,0,10,<u>13</u>,<u>13</u>,16,16]
* 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,<u>16</u>,<u>16</u>]
* 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.
*
* Example 3:
*
* Input: s = "6+0*1", answers = [12,9,6,4,8,6]
* Output: 10
* Explanation: The correct answer of the expression is 6.
* If a student had incorrectly done (6+0)*1, the answer would also be 6.
* By the rules of grading, the students will still be rewarded 5 points (as they got the correct answer), not 2 points.
* The points for the students are: [0,0,5,0,0,5]. The sum of the points is 10.
*
*
* 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].
* n == answers.length
* 1 <= n <= 10^4
* 0 <= answers[i] <= 1000
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/the-score-of-students-solving-math-expression/
// discuss: https://leetcode.com/problems/the-score-of-students-solving-math-expression/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn score_of_students(s: String, answers: Vec<i32>) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2019_example_1() {
let s = "7+3*1*2".to_string();
let answers = vec![20, 13, 42];
let result = 7;
assert_eq!(Solution::score_of_students(s, answers), result);
}
#[test]
#[ignore]
fn test_2019_example_2() {
let s = "3+5*2".to_string();
let answers = vec![13, 0, 10, 13, 13, 16, 16];
let result = 19;
assert_eq!(Solution::score_of_students(s, answers), result);
}
#[test]
#[ignore]
fn test_2019_example_3() {
let s = "6+0*1".to_string();
let answers = vec![12, 9, 6, 4, 8, 6];
let result = 10;
assert_eq!(Solution::score_of_students(s, answers), result);
}
}
// Accepted solution for LeetCode #2019: The Score of Students Solving Math Expression
function scoreOfStudents(s: string, answers: number[]): number {
const n = s.length;
const cal = (s: string): number => {
let res = 0;
let pre = s.charCodeAt(0) - '0'.charCodeAt(0);
for (let i = 1; i < s.length; i += 2) {
const cur = s.charCodeAt(i + 1) - '0'.charCodeAt(0);
if (s[i] === '+') {
res += pre;
pre = cur;
} else {
pre *= cur;
}
}
res += pre;
return res;
};
const x = cal(s);
const m = (n + 1) >> 1;
const f: Set<number>[][] = Array(m)
.fill(0)
.map(() =>
Array(m)
.fill(0)
.map(() => new Set()),
);
for (let i = 0; i < m; ++i) {
f[i][i].add(s[i << 1].charCodeAt(0) - '0'.charCodeAt(0));
}
for (let i = m - 1; i >= 0; --i) {
for (let j = i; j < m; ++j) {
for (let k = i; k < j; ++k) {
for (const l of f[i][k]) {
for (const r of f[k + 1][j]) {
const op = s[(k << 1) + 1];
if (op === '+' && l + r <= 1000) {
f[i][j].add(l + r);
} else if (op === '*' && l * r <= 1000) {
f[i][j].add(l * r);
}
}
}
}
}
}
const cnt: number[] = Array(1001).fill(0);
for (const v of answers) {
++cnt[v];
}
let ans = cnt[x] * 5;
for (let i = 0; i <= 1000; ++i) {
if (i !== x && f[0][m - 1].has(i)) {
ans += cnt[i] << 1;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.