Rings on Rods
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.
Input
s: a corrent string of length 2N describing the N rings put on rods
Output
how many points you will get
Examples
Example 1:
Input:
s = B2R5G2R2
Output:
1
Explanation:
One point for rings put on the second rod.
int solution(std::string s) {
int res=0;
//R=0,G=1,B=2;
int a[11][4];
memset(a,0,sizeof(a));
for(int i=0; i<s.size()-1; i+=2){
int temp = 0;
int rod = s[i+1]-'0';
if(s[i]=='R') temp = 0;
else if(s[i]=='G') temp = 1;
else if(s[i]=='B') temp = 2;
a[rod][temp]++;
if(a[rod][0]==1 && a[rod][1]==1 && a[rod][2]==1) res++;
}
return res;
}