Google Assessment Question || Online || 10 Rods
Anonymous User
1452

You have 10 rods numbered from 0 to 9. There are three types of rings—red,
green and blue—being put on the rods. You get a point for each rod that has a
ring of each color on it, i.e. to get a point you need a red ring, green ring and blue ring on one rod.

    A ring put on a rod is represented by two characters—the first
    describes the color of the ring and the second describes which number
    rod it is on from 0 to 9. For example "R8" means that a red ring is being put on the 8th rod.


    Write a function:

class Solution { public int solution(String S); }

that, given a correct string of length 2N describing the N rings
put onto rods, returns how many points you will get.

    Examples:
    For ""B2R5G2R2,"" the answer is 1
    Given S = "R8R0B5G1B8G8", your answer should be 1.
    You get one point for rings put on the 8th rod (there is one red, one blue and one green ring on it). There is also a red ring on the 0th rod, green on the 1st rod and blue on the 5th rod. You don't get points for them because they don't form a full trio on one rod. There are no rings on any of the other rods.
    All changes saved
    Keyboard navigation: Use Tab to advance the cursor. To exit the editor, press the ctrl and [ keys.

class Solution {

public static int solution(String S) {
    System.err.println("Tip: Use System.err.println() to write debug messages on the output tab.");
    return 0;
}

}

class Solution {

public static int solution(String S) {
    
    System.err.println("Tip: Use System.err.println() to write debug messages on the output tab.");
    
    return 0;
    
}

}

The purpose of this question is being able to count the points each rod from 0-9 if they contain all 3 colored rings. For example, if the string has a number 2 on it and contains all colors (green, blue, and red) that counts as one point.

this is my approach **

'''
List rods = new ArrayList<>(9);

    int equalsOnePoint = 0;
    int blue = 1, green = 1, red = 1;
    int totalPoints = 0;
    String string = "";

    for (int i = 0; i < S.toLowerCase().length(); i++) {
        string += (S.charAt(i));

        if ((!Character.isLetter(S.charAt(i)))) {
            rods.add(string.toString());
            string = "";
        }
    }

    for (int i = 0; i < rods.size(); i++) {
        if (rods.get(i).contains("B")) {
            blue--;
        }
        if (rods.get(i).contains("G")) {
            green--;
        }
        if (rods.get(i).contains("R")) {
            red--;
        }
        if (blue + green + red == equalsOnePoint) {
            totalPoints++;
            blue = 1;
            green = 1;
            red = 1;
        }
    }


    System.out.println(rods);
    System.out.println(totalPoints);
    return totalPoints;

'''

Comments (0)