You are given a string array events.
Initially, score = 0 and counter = 0. Each element in events is one of the following:
"0", "1", "2", "3", "4", "6": Add that value to the total score."W": Increase the counter by 1. No score is added."WD": Add 1 to the total score."NB": Add 1 to the total score.Process the array from left to right. Stop processing when either:
events have been processed, orReturn an integer array [score, counter], where:
score is the final total score.counter is the final counter value.
Example 1:
Input: events = ["1","4","W","6","WD"]
Output: [12,1]
Explanation:
| Event | Score | Counter |
|---|---|---|
"1" |
1 | 0 |
"4" |
5 | 0 |
"W" |
5 | 1 |
"6" |
11 | 1 |
"WD" |
12 | 1 |
Final result: [12, 1].
Example 2:
Input: events = ["WD","NB","0","4","4"]
Output: [10,0]
Explanation:
| Event | Score | Counter |
|---|---|---|
"WD" |
1 | 0 |
"NB" |
2 | 0 |
"0" |
2 | 0 |
"4" |
6 | 0 |
"4" |
10 | 0 |
Final result: [10, 0].
Example 3:
Input: events = ["W","W","W","W","W","W","W","W","W","W","W"]
Output: [0,10]
Explanation:
After 10 occurrences of "W", the counter reaches 10, so processing stops. The remaining events are ignored.
Constraints:
1 <= events.length <= 1000events[i] is one of "0", "1", "2", "3", "4", "6", "W", "WD", or "NB".Problem Overview: You are given a reported total score and a list of individual scoring events. The task is to verify whether the recorded total is valid based on the events provided. If the sum of all scoring events matches the reported score (and follows any basic constraints like non‑negative scoring), the score is considered valid.
Approach 1: Recompute Total (Brute Force) (Time: O(n), Space: O(1))
The most direct approach recomputes the score from scratch. Iterate through the list of scoring events and accumulate their values into a running total. After processing all events, compare the computed sum with the reported score. If they match and each event value satisfies basic constraints (such as non-negative or within allowed limits), the score is valid. This approach uses simple iteration and arithmetic, making it easy to implement and reason about.
Approach 2: Single-Pass Validation (Optimal) (Time: O(n), Space: O(1))
A slightly more defensive version performs validation during the same traversal. Iterate through the events while maintaining a running sum. At each step, check whether the event value is within the allowed scoring range and update the cumulative score. If any invalid value appears, terminate early. After the traversal finishes, verify that the cumulative sum equals the reported total. This still runs in linear time but catches invalid inputs earlier, which is useful when working with large datasets.
Approach 3: Prefix Tracking for Streaming Data (Time: O(n), Space: O(1))
If the scoring events arrive as a stream rather than a fixed list, maintain a running prefix sum and validate each update as it arrives. Each new event is added to the current total, and the system checks constraints immediately. This pattern is common in real-time score tracking systems and uses the same constant memory footprint while processing events sequentially.
Recommended for interviews: The single-pass validation approach is what interviewers typically expect. It demonstrates clear reasoning: iterate once, maintain a running total, and verify constraints while computing the final score. The brute force recomputation also works but mainly shows the baseline idea. Understanding linear scans and cumulative calculations—core techniques in arrays and iteration problems—helps you quickly recognize and solve similar validation tasks.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recompute Total (Brute Force) | O(n) | O(1) | Simple validation when you only need to check the final total |
| Single-Pass Validation | O(n) | O(1) | General case where you want to detect invalid events early |
| Streaming Prefix Tracking | O(n) | O(1) | When events arrive incrementally in a stream or live system |
Practice Score Validator with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor