Skip to content

3921. Score Validator

Description

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 events have been processed, or
  • The counter becomes 10.

Return 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 <= 1000
  • events[i] is one of "0", "1", "2", "3", "4", "6", "W", "WD", or "NB".

Solutions

Solution 1: Simulation

We can directly simulate the process described in the problem to calculate the final score and counter value.

First, we initialize two variables \(\textit{score}\) and \(\textit{counter}\), representing the current total score and counter value respectively. Then we iterate through each event in the array \(\textit{events}\) and update \(\textit{score}\) and \(\textit{counter}\) based on the event type:

  • If the event is a numeric string, we convert it to an integer and add it to \(\textit{score}\).
  • If the event is the string "W", we increment \(\textit{counter}\) by 1 and check if it has reached 10; if so, we stop processing.
  • Otherwise (the event is "WD" or "NB"), we add 1 to \(\textit{score}\).

After processing all events or when the counter reaches 10, we return an array containing the final values of \(\textit{score}\) and \(\textit{counter}\).

The time complexity is \(O(n)\), where \(n\) is the length of the array \(\textit{events}\). The space complexity is \(O(1)\), as we only use a constant amount of extra space.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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};
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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};
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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}
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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];
}

Comments