Score Validator solution and explanation
5 more videos on this problem
Score Validator | Leetcode Biweekly Contest 182 | Q1 | Leetcode 3921 | Array | Simulation | EasyPragya Gupta · 3:48 · 134 views
Score Validator | Leetcode bi-weekly contest 182 | #3921UMANG AGRAWAL · 4:34 · 9 views
LeetCode 3921 | Score Validator | Cricket Simulation Explained | C++ SolutionPatel Jivan · 5:40 · 8 views
LeetCode 3921 Score Validator | Easy C++ Solution ExplainedGrowMoree · 13:01 · 3 views
LeetCode 3921 - Score Validator | Easy Simulation Problem | C++ SolutionTapan Vachhani · 4:47 · 3 views
LeetCode 3921 in 0ms 🚀 | Score Validator | Easy C++ SolutionRishab Chandgothia · 4:15 · 1 view
The problem
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.
class Solution:
def scoreValidator(self, events: list[str]) -> list[int]:
score = counter = 0
for event in events:
if event.isdigit():
score += int(event)
elif event == "W":
counter += 1
if counter == 10:
break
else:
score += 1
return [score, counter]class Solution {
public int[] scoreValidator(String[] events) {
int score = 0;
int counter = 0;
for (String event : events) {
if (event.matches("\\d+")) {
score += Integer.parseInt(event);
} else if (event.equals("W")) {
if (++counter == 10) {
break;
}
} else {
score++;
}
}
return new int[] {score, counter};
}
}class Solution {
public:
vector<int> scoreValidator(vector<string>& events) {
int score = 0;
int counter = 0;
for (string event : events) {
if (isdigit(event[0])) {
score += stoi(event);
} else if (event == "W") {
if (++counter == 10) {
break;
}
} else {
score++;
}
}
return {score, counter};
}
};function scoreValidator(events: string[]): number[] {
let score = 0;
let counter = 0;
for (const event of events) {
if (/^\d+$/.test(event)) {
score += parseInt(event);
} else if (event === 'W') {
counter++;
if (counter === 10) {
break;
}
} else {
score++;
}
}
return [score, counter];
}func scoreValidator(events []string) []int {
score := 0
counter := 0
for _, event := range events {
if num, err := strconv.Atoi(event); err == nil {
score += num
} else if event == "W" {
counter++
if counter == 10 {
break
}
} else {
score++
}
}
return []int{score, counter}
}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 |
Frequently asked questions
Is Score Validator easy or hard?
Score Validator is generally considered an easy problem. The core idea is verifying a computed total using a linear scan through the input array. It focuses on basic iteration, arithmetic checks, and careful validation of constraints.
Score Validator Python/Java solution
In both Python and Java, the solution typically loops through the array of events, adds each value to a running total, and checks constraints along the way. After the loop, compare the calculated sum with the reported score. The logic is identical across languages and runs in O(n) time with O(1) space.
How to solve Score Validator in O(n)?
Traverse the scoring events once while maintaining a cumulative sum. During the iteration, validate that each event value falls within allowed constraints and update the running total. After the traversal, compare the calculated sum with the reported score to determine validity.
What is the best approach for Score Validator?
The best approach is a single-pass validation. Iterate through the list of scoring events, maintain a running sum, and verify that each event value is valid. After processing all events, check if the computed sum matches the reported score. This method runs in O(n) time and uses O(1) space.
Is Score Validator asked at Google/Amazon/Meta?
Problems that involve validating totals, cumulative sums, and input constraints appear frequently in interviews at companies like Amazon and Google. While the exact problem name may vary, the underlying pattern—single-pass validation over an array—is common in coding interviews.
What data structure is used in Score Validator?
The primary data structure is an array or list storing the scoring events. The algorithm processes the array sequentially and maintains a running total using simple variables, which keeps space usage constant.
What is the time complexity of Score Validator?
The typical solution runs in O(n) time, where n is the number of scoring events. Each event is processed exactly once while updating a running total. The algorithm uses O(1) additional space because it only stores a few counters.
