Score Validator - Solution & Explanation
Problem Statement
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:
- All elements in
eventshave been processed, or - The counter becomes 10.
Return an integer array [score, counter], where:
scoreis the final total score.counteris 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".
Approach Overview
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.
Solution
We can directly simulate the process described in the problem to calculate the final score and counter value.
First, we initialize two variables score and counter, representing the current total score and counter value respectively. Then we iterate through each event in the array events and update score and counter based on the event type:
- If the event is a numeric string, we convert it to an integer and add it to
score. - If the event is the string
"W", we incrementcounterby 1 and check if it has reached 10; if so, we stop processing. - Otherwise (the event is
"WD"or"NB"), we add 1 toscore.
After processing all events or when the counter reaches 10, we return an array containing the final values of score and counter.
The time complexity is O(n), where n is the length of the array events. The space complexity is O(1), as we only use a constant amount of extra space.
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| 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 |
Video Solution
Score Validator | Leetcode Biweekly Contest 182 | Q1 | Leetcode 3921 | Array | Simulation | Easy • Pragya Gupta • 134 views views
Watch 5 more video solutions →Frequently Asked Questions
Is Score Validator easy or hard?
Score Validator Python/Java solution
How to solve Score Validator in O(n)?
What is the best approach for Score Validator?
Is Score Validator asked at Google/Amazon/Meta?
What data structure is used in Score Validator?
What is the time complexity of Score Validator?
Ready to solve this problem?
Practice Score Validator with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor