LeetCode-in-Java

3921. Score Validator

Easy

You are given a string array events.

Initially, score = 0 and counter = 0. Each element in events is one of the following:

Process the array from left to right. Stop processing when either:

Return an integer array [score, counter], where:

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:

Solution

public class Solution {
    public int[] scoreValidator(String[] events) {
        int counter = 0;
        int score = 0;
        for (String i : events) {
            if (counter == 10) {
                break;
            }
            if (i.equals("WD")) {
                score += 1;
            } else if (i.equals("NB")) {
                score += 1;
            } else if (i.equals("W")) {
                counter += 1;
            } else {
                score += Integer.parseInt(i);
            }
        }
        return new int[] {score, counter};
    }
}