Google Online Assessment 2021 (OA) - Rings on Rods - Company-specific OAs / Google OA

https://algo.monster/problems/google-oa-rings-on-rods

share my solution

def solution(s: str) → int:
# WRITE YOUR BRILLIANT CODE HERE

arr = [set() for _ in range(10)]

for i in range(0, len(s), 2):
    arr[int(s[i + 1])].add(s[i])

return sum((len(arr[i]) == 3 for i in range(len(arr))))

No conversion to int required.

def solution(s: str) → int:
# WRITE YOUR BRILLIANT CODE HERE
i, bset, rset, gset = 0, set(),set(),set()
while(i in range(len(s)-1)):
if s[i] == ‘B’:
bset.add(s[i+1])
if s[i] == ‘G’:
gset.add(s[i+1])
if s[i] == ‘R’:
rset.add(s[i+1])
i += 1
return len(bset & gset & rset)

def solution(s: str) → int:
# WRITE YOUR BRILLIANT CODE HERE
mp=defaultdict(set)
count=0

for i in range(0,len(s),2):
    color, rod=s[i],s[i+1]
    mp[rod].add(color)
    if len(mp[rod]) == 3:
        count+=1

return count

I have a question: do you get a point per rod or can you have multiple points per rod?

Solution for javascript
function solution(s) {
// create an array of objects representing each rod in zero
let rods = new Array(10).fill(0).map(el => {el = {‘R’:0,‘G’:0,‘B’:0}; return el});

// read string and fill rods with rings
for (let index = 0; index < s.length; index+=2) {
const ring = s[index];
const rod = s[index+1];
rods[rod][ring] +=1;
}
// count and return the points
return rods.reduce((prev,curr)=>{
prev+= (curr.R > 0 && curr.G >0 && curr.B > 0) ? 1:0;
return prev
},0)
}

my solution with hashmaps:

def solution(s: str) -> int:
    rings = {}
    for idx in range(len(s)-1):
        c, p = s[idx], s[idx+1]
        rings.setdefault(p,[])
        rings[p].append(c)
    points = 0
    for k,v in rings.items():
        if len(set(v))>2:
            points+=1
    return points