Nice Garland (Codeforces 1108C)
Tags: Greedy, brute force

You have a garland consisting of $$$n$$$ lamps. Each lamp is colored red, green or blue. The color of the $$$i$$$-th lamp is $$$s_i$$$ ('R', 'G' and 'B' — colors of lamps in the garland).You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is nice.A garland is called nice if any two lamps of the same color have distance divisible by three between them. I.e. if the obtained garland is $$$t$$$, then for each $$$i, j$$$ such that $$$t_i = t_j$$$ should be satisfied $$$|i-j|~ mod~ 3 = 0$$$. The value $$$|x|$$$ means absolute value of $$$x$$$, the operation $$$x~ mod~ y$$$ means remainder of $$$x$$$ when divided by $$$y$$$.For example, the following garlands are nice: "RGBRGBRG", "GB", "R", "GRBGRBG", "BRGBRGB". The following garlands are not nice: "RR", "RGBG".Among all ways to recolor the initial garland to make it nice you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.

Input: The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of lamps.The second line of the input contains the string $$$s$$$ consisting of $$$n$$$ characters 'R', 'G' and 'B' — colors of lamps in the garland.

Output: In the first line of the output print one integer $$$r$$$ — the minimum number of recolors needed to obtain a nice garland from the given one.In the second line of the output print one string $$$t$$$ of length $$$n$$$ — a nice garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.


Sample Inputs:
3
BRB

7
RGBGRBB

Sample Outputs:
1
GRB

3
RGBRGBR

Solution (Python):
size = int(input())
word = input()
(R, B, G) = ('R', 'B', 'G')

def build(l1, l2, l3):
    return (l1 + l2 + l3) * (size // 3) + (l1 if size % 3 == 1 else '') + (l1 + l2 if size % 3 == 2 else '')
w0 = word[::3]
w1 = word[1::3]
w2 = word[2::3]
(r0, b0, g0) = (w0.count('R'), w0.count('B'), w0.count('G'))
(r1, b1, g1) = (w1.count('R'), w1.count('B'), w1.count('G'))
(r2, b2, g2) = (w2.count('R'), w2.count('B'), w2.count('G'))
(s1, s2, s3, s4, s5, s6) = (r0 + b1 + g2, r0 + b2 + g1, r1 + b0 + g2, r1 + b2 + g0, r2 + b1 + g0, r2 + b0 + g1)
(s1, s2, s3, s4, s5, s6) = (size - s1, size - s2, size - s3, size - s4, size - s5, size - s6)
print(min(s1, s2, s3, s4, s5, s6))
if s1 == min(s1, s2, s3, s4, s5, s6):
    print(build(R, B, G))
elif s2 == min(s1, s2, s3, s4, s5, s6):
    print(build(R, G, B))
elif s3 == min(s1, s2, s3, s4, s5, s6):
    print(build(B, R, G))
elif s4 == min(s1, s2, s3, s4, s5, s6):
    print(build(G, R, B))
elif s5 == min(s1, s2, s3, s4, s5, s6):
    print(build(G, B, R))
elif s6 == min(s1, s2, s3, s4, s5, s6):
    print(build(B, G, R))

Solution (Python):
from collections import Counter
from itertools import permutations
from math import inf
n = int(input())
s = input()
(c1, c2, c3) = (Counter(s[0::3]), Counter(s[1::3]), Counter(s[2::3]))
t1 = n // 3
(t2, t3) = (t1, t1)
if n % 3 > 0:
    t1 += 1
if n % 3 > 1:
    t2 += 1
best = None
b_score = inf
for perm in permutations(['R', 'G', 'B']):
    score = 0
    score += t1 - c1[perm[0]]
    score += t2 - c2[perm[1]]
    score += t3 - c3[perm[2]]
    if score < b_score:
        b_score = score
        best = perm
print(b_score)
print(''.join(best * t1)[:n])

Solution (Python):
import collections
import itertools
(n, s) = (int(input()), input())
c = []
for i in range(3):
    t = collections.Counter(s[i::3])
    c.append({color: t[color] for color in 'RGB'})
    c[i]['ALL'] = sum(t.values())
m = min([(sum([c[i]['ALL'] - c[i][colors[i]] for i in range(3)]), colors) for colors in itertools.permutations('RGB', 3)], key=lambda x: x[0])
print(m[0])
print(''.join((m[1] * (n // 3 + 1))[:n]))