Adjacent Colors
InterviewTime limit1sMemory limit1024 MB
For each circular string of R, G, and B beads, find the color pair that is adjacent most often, printing every pair that ties for the maximum.
- Level
Easy2 of 10
- Topics
- String, Implementation
- Solved
- No attempts yet
Problem
An ethnographer studies a people of the far north. He is interested in the structure of necklaces made of beads in three colors: blue, green, and red. He believes that how often certain colors appear next to each other tells us something about the culture of the people.
A necklace is given as a string of uppercase letters. R is a red bead, G is a green bead, and B is a blue bead. Adjacent letters in the string are adjacent beads. The beads are strung on a circular thread, so the first and last beads are also adjacent.
For example, in the necklace RGRGRGRG, green and red beads are adjacent 8 times. In the necklace RRRR, two red beads are adjacent 4 times.
Help the ethnographer find which pair of colors is most often adjacent in his bead samples. The order of the colors in a pair does not matter.
Input
The first line contains the number of necklaces the ethnographer has (). Each of the following lines contains one string of the letters R, G, and B. The length of each string is at least and at most .
Output
Print one or more lines, each with two characters: the colors of the two beads that are most often adjacent. If several pairs are adjacent equally often and are the most frequent, print all of them, one pair per line, in any order. The colors within a pair can be printed in any order.