Given a pixel sequence consisting of red, green and blue pixels find the count of all contiguous subsequences such that each subsequence has a minimum length of 3 and the sequence cycles between red r, green g and blue b. For eg. if the sequence starts with g then the next character in the sequence should be b and the next should be r and so on.
Input Format
First line of test case consists of an integer t denoting the number of test cases. Next t test cases follow. Each test case consists of two lines. First line of each test case is the length of the pixel sequence. Second line is the pixel sequence.
Sample Input
5
3
rgb
4
brgb
2
rg
1
b
16
rgbrbbrgbgbrgrgb
Sample Output
1
3
0
0
10
Constraints
1 <= t <= 1000
1 <= |s| <= 1000
character set of s - rgb
Explanation
For the character sequence rgbrbbrgbgbrgrgb, there are 10 possible contiguous subsequences viz. rgb, rgbr, gbr, brg, brgb, rgb, gbr, gbrg, brg, rgb which satisfy the condition.
Answers
Explanation:
Red, Green and Blue Given a pixel sequence consisting of red, green and blue pixels find the count of all contiguous subsequences such that each subsequence has a minimum length of 3 and the sequence cycles between red r, green g and blue b. For eg. if the sequence starts with g then the next character in the sequence should be b and the next should be r and so on. Input Format First line of test case consists of an integer t denoting the number of test cases. Next t test cases follow. Each test case consists of two lines. First line of each test case is the length of the pixel sequence. Second line is the pixel sequence. Sample Input rgb brgb rgbrbbrgbgbrgrgb Sample Output
Answer:
Cannot attach the code here, so I have attached the link to the solution from GitHub.
https://github.com/Rathan-Naik/ProblemSolving/blob/master/ColorSequenceSolution.java
Explanation:
Start from 0th index, Keep incrementing the count until the sequence breaks
Once sequence breaks, if sequence length is greater than 3 (RGB or similar sequence) then only use the following formula.
count * (count-3)/2 add it to total sum.
Add 1 to this because whole string is also a sequence.
Restart the new sequence from the index where we left last till the end of string.