Problem:


Score : 400 points

Problem Statement

We have a string S of length N consisting of R, G, and B.

Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:

S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k.

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

S is a string of length N consisting of R, G, and B.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800



Sample input:
4
RRGB


Sample output:
1

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import Counter
sys.setrecursionlimit(10**8)
INF = float("inf")


def solve(N: int, S: str):
    c = Counter(S)
    count = c["R"] * c["G"]*c["B"]
    for i in range(1, (N-3)//2+2):
        for j in range(N):
            if j+2*i >= N:
                break
            if not (S[j] == S[j+i] or S[j] == S[j+i+i] or S[j+i] == S[j+i+i]):
                count -= 1
    print(count)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:


def resolve():
    N = int(input())
    R=set()
    G=set()
    B=set()
    S = input()
    for index,i in enumerate(S):
        if i == "R":
            R.add(index)
        elif i == "G":
            G.add(index)
        else:
            B.add(index)
    count = len(R)* len(G)* len(B)

    for red in R:
        for blue in B:
            mx = max(red,blue)
            mn = min(blue,red)
            step = mx - mn


            if (mx + step) in G:
                count -= 1
            
            if (mn - step) in G:
                count -= 1
            if step % 2 ==0 and (mn + step // 2) in G:
                count -=1

    print(count)



                

            







if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
N = int(input())
S = input()

r = S.count('R')
g = S.count('G')
b = S.count('B')
ans = r * g * b
for j in range(1, N - 1):
    for v in range(1, min(j+1, N - j)):
        if S[j - v] != S[j] and S[j + v] != S[j] and S[j - v] != S[j + v]:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    from collections import Counter
    c = Counter(S)
    ans = 1 if len(c.keys()) > 2 else 0
    for v in c.values():
        ans *= v
    less = 0
    for d in range(1, N+1):
        for i in range(N):
            if i+d < N and i+2*d < N and \
                    S[i] != S[i+d] and S[i] != S[i+2*d] and S[i+d] != S[i+2*d]:
                less += 1
    print(ans - less)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = input()

C = Counter(S)
ans = C['R']*C['G']*C['B']

for w in range(1, (N+1)//2+1):
    for i in range(N-(w*2)):
        if S[i]!=S[i+w] and S[i]!=S[i+2*w] and S[i+w]!=S[i+2*w]:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()

total = 0
for j in range(1, n-1):
    left_d = {"R": set(), "G": set(), "B": set()}
    for k in range(j+1, n):
        left_d[s[k]].add(k-j)
    for i in range(j):
        if s[j] == s[i]:
            continue
        if s[i] == "R" and s[j] == "G" or s[i] == "G" and s[j] == "R":
            target = "B"
        if s[i] == "R" and s[j] == "B" or s[i] == "B" and s[j] == "R":
            target = "G"
        if s[i] == "B" and s[j] == "G" or s[i] == "G" and s[j] == "B":
            target = "R"
        total += len(left_d[target])
        if j-i in left_d[target]:
            total -= 1
        # print(i, j, total, left_d)

print(total)

Submission status: Accepted

Solution code:
N=int(input())
S=list(input())
counts = 0
tot = 0
r = [0]*N
b = [0]*N
g = [0]*N
R=0
B=0
G=0
"""
if S[0] == "R":
    r[0] +=1
if S[0] == "G":
    g[0] +=1
if S[0] == "B":
    b[0] +=1

"""
for i in range(N):
    if S[i] == "R":
        R+=1

    if S[i] == "B":
        B+=1
        
    if S[i] == "G":
        G+=1



        

for i in range(N):
    for j in range(1,(N-i-1)//2+1):
        if S[i] != S[i+j] and S[i+j] != S[i+2*j] and S[i] != S[i+2*j]:
            counts +=1


print(R*B*G-counts)

Submission status: Accepted

Solution code:
N, = (int(x) for x in input().split())
S, = ((x) for x in input().split())


import collections

cc = collections.Counter(S)
if len(cc) != 3:
    sol = 0
else:
    sol = 1
    for x in cc.values():
        sol *= x

for i in range(N):
    for j in range(i + 2, N, 2):
        if S[i] == S[j]:
            continue
        k = (i + j) // 2
        if S[k] not in (S[i], S[j]):
            sol -= 1

print(sol)

Submission status: Accepted

Solution code:
n = int(input())
string = input().strip()
r = string.count('R')
g = string.count('G')
h = string.count('B')

ans = r * g * h

if ans == 0:
    print(ans)
else:
    for i in range(n):
        j = 1
        while i + 2 * j < n:
            if string[i] != string[i + j] and string[i + j] != string[i + 2*j] and string[i] != string[i+2*j]:
                ans -= 1
            j += 1
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
R = 0
G = 0
B = 0
for i in S:
    if i == 'R':
        R += 1
    elif i == 'G':
        G += 1
    elif i == 'B':
        B += 1
#print(R,G,B)
RGBall = R*G*B
if N % 2 == 0:
    X = N // 2 - 1
else:
    X = N // 2
eql = 0
for i in range(1,X+1):
    for j in range(N):
        a = S[j]
        b = S[j+i]
        c = S[j+2*i]
        #print(a,b,c)
        if a != b and b != c and c != a:
            eql += 1
        if j + 2*i == N-1:
            break
#print(RGBall,eql)
ans = RGBall - eql
print(ans)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_right as br
from bisect import bisect_left as bl
input = sys.stdin.readline
N = int(input())
S = list(input())[: -1]
rs = []
gs = []
bs = []
res = 0
for i in range(N):
  if S[i] == "R": rs.append(i)
  if S[i] == "G": gs.append(i)
  if S[i] == "B": bs.append(i)
#print(rs, gs, bs)
for i in range(N):
  for j in range(i + 1, N):
    if S[i] == S[j]: continue
    d = j - i
    k = 0
    x = 0
    t = 0
    if S[i] == "R":
      if S[j] == "G":
        k = br(bs, j)
        x = bl(bs, j + d)
        t = len(bs) - k
        if len(bs) > x:
          t -= (j + d) == bs[x]
      elif S[j] == "B": 
        k = br(gs, j)
        x = bl(gs, j + d)
        t = len(gs) - k
        if len(gs) > x:
          t -= (j + d) == gs[x]
    elif S[i] == "G":
      if S[j] == "R":
        k = br(bs, j)
        x = bl(bs, j + d)
        t = len(bs) - k
        if len(bs) > x:
          t -= (j + d) == bs[x]
      elif S[j] == "B":
        k = br(rs, j)
        x = bl(rs, j + d)
        t = len(rs) - k
        if len(rs) > x:
          t -= (j + d) == rs[x]
    elif S[i] == "B":
      if S[j] == "R":
        k = br(gs, j)
        x = bl(gs, j + d)
        t = len(gs) - k
        if len(gs) > x:
          t -= (j + d) == gs[x]
      elif S[j] == "G":
        k = br(rs, j)
        x = bl(rs, j + d)
        t = len(rs) - k
        if len(rs) > x:
          t -= (j + d) == rs[x]
    res += t
    #print(i, j, t, k, x)
print(res)

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline

N = int(input())
S = input().rstrip()

ans = 0

R = 0
G = 0
B = 0

for i in range(N):
    if S[i] == "R":
        R += 1
    elif S[i] == "G":
        G += 1
    elif S[i] == "B":
        B += 1

ans += R * G * B

for i in range(N):
    for j in range(i + 1, N):
        k = 2 * j - i
        if k <= j or k >= N:
            continue
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S = input()

Lr = [0]*(N+2)
Lg = [0]*(N+2)
Lb = [0]*(N+2)
Rr = [0]*(N+2)
Rg = [0]*(N+2)
Rb = [0]*(N+2)
for i,s in enumerate(S):
    Lr[i+1] = Lr[i]
    Lg[i+1] = Lg[i]
    Lb[i+1] = Lb[i]
    if s=="R":
        Lr[i+1] += 1
    elif s=="G":
        Lg[i+1] += 1
    elif s=="B":
        Lb[i+1] += 1

for i,s in enumerate(S[::-1]):
    i = N-i
    Rr[i] = Rr[i+1]
    Rg[i] = Rg[i+1]
    Rb[i] = Rb[i+1]
    if s=="R":
        Rr[i] += 1
    elif s=="G":
        Rg[i] += 1
    elif s=="B":
        Rb[i] += 1

ans = 0
for i,s in enumerate(S):
    if s=="R":
        ans += Lg[i+1]*Rb[i+1] + Lb[i+1]*Rg[i+1]
        for j in range(1,N+1):
            if i-j < 0 or i+j >= N: break
            if (S[i-j] == "B" and S[i+j] == "G") or (S[i-j] == "G" and S[i+j] == "B"):
                ans -= 1
    elif s=="G":
        ans += Lr[i+1]*Rb[i+1] + Lb[i+1]*Rr[i+1]
        for j in range(1,N+1):
            if i-j < 0 or i+j >= N: break
            if (S[i-j] == "B" and S[i+j] == "R") or (S[i-j] == "R" and S[i+j] == "B"):
                ans -= 1
    elif s=="B":
        ans += Lg[i+1]*Rr[i+1] + Lr[i+1]*Rg[i+1]
        for j in range(1,N+1):
            if i-j < 0 or i+j >= N: break
            if (S[i-j] == "R" and S[i+j] == "G") or (S[i-j] == "G" and S[i+j] == "R"):
                ans -= 1
print(ans)

Submission status: Accepted

Solution code:
def f(x,y,z):
    ans=0
    for p in x:
        for q in y:
            if p<q and 2*q-p in z: ans+=1
            elif q<p and 2*p-q in z: ans+=1
    return ans

n,s=int(input()),input()
r,g,b=set(),set(),set()
for i in range(n):
    if s[i]=="R": r.add(i)
    elif s[i]=="G": g.add(i)
    else: b.add(i)
print(len(r)*len(g)*len(b)-(f(r,g,b)+f(r,b,g)+f(g,b,r)) )

Submission status: Accepted

Solution code:
N = int(input())
S = input()

r = S.count("R")
g = S.count("G")
b = S.count("B")

ans = r * g * b
for i in range(1, N):
    if 2 * i + 1 > N:
        break
    for x in range(N):
        if x + 2 * i + 1 > N:
            break
        if S[x] != S[x+i] and S[x] != S[x+(2*i)] and S[x+i] != S[x+(2*i)]:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
def main():
    from bisect import bisect_left

    N = int(input())
    s = input()

    Rs = []
    Gs = []
    Bs = []
    for i, c in enumerate(s):
        if c == 'R':
            Rs.append(i)
        elif c == 'G':
            Gs.append(i)
        else:
            Bs.append(i)

    s += 'Z' * 4000

    ans = 0
    for mid in Rs:
        for g in Gs:
            if g >= mid: break
            ans += len(Bs) - bisect_left(Bs, mid) - (1 if s[mid * 2 - g] == 'B' else 0)
        for b in Bs:
            if b >= mid: break
            ans += len(Gs) - bisect_left(Gs, mid) - (1 if s[mid * 2 - b] == 'G' else 0)
    for mid in Gs:
        for r in Rs:
            if r >= mid: break
            ans += len(Bs) - bisect_left(Bs, mid) - (1 if s[mid * 2 - r] == 'B' else 0)
        for b in Bs:
            if b >= mid: break
            ans += len(Rs) - bisect_left(Rs, mid) - (1 if s[mid * 2 - b] == 'R' else 0)
    for mid in Bs:
        for r in Rs:
            if r >= mid: break
            ans += len(Gs) - bisect_left(Gs, mid) - (1 if s[mid * 2 - r] == 'G' else 0)
        for g in Gs:
            if g >= mid: break
            ans += len(Rs) - bisect_left(Rs, mid) - (1 if s[mid * 2 - g] == 'R' else 0)
    print(ans)


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = input()
cnt = Counter(s)
ans = cnt["R"] * cnt["G"] * cnt["B"]

# 端固定
rgb = 0
for i in range(n-1):
    for j in range(i+1, n):
        if 2 * j > n - 1 + i:
            break
        if s[i] != s[j] and s[j] != s[2 * j - i] and s[i] != s[2 * j - i]:
            rgb += 1

print(ans-rgb)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
r = 0
g = 0
b = 0

for i in S:
    if i == "R":
        r += 1
    if i == "G":
        g += 1
    if i == "B":
        b += 1

ans = r*g*b

for i in range(N):
    for j in range(i+1,N):
        if 2*j - i <= N - 1:
            t = [ S[i],S[j],S[2*j-i] ]
            if len(set(t)) == 3:
                ans -= 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r, g, b = 0, 0, 0
for c in s:
    if c == "R": r += 1
    elif c == "G": g += 1
    else: b += 1

ans = r * g * b
for i in range(n-1):
    for j in range(i+1, n):
        k = j + j-i
        if k >= n: continue
        if s[i] != s[j] and s[j] != s[k] and s[i] != s[k]: ans -= 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

rn = S.count('R')
gn = S.count('G')
bn = S.count('B')

eqn = 0
for i in range(N):
    for j in range(i+1, N):
        k = j + (j-i)
        if k < N and S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
            eqn += 1

ans = rn * gn * bn - eqn

print(ans)

Submission status: Accepted

Solution code:


N = int(input())
s = input()
res = 0
for j in range(1,N-1):
    char = s[j]
    left_list = ['B', 'R']
    right_list = ['R', 'B']
    if char == 'B':
        left_list = ['G', 'R']
        right_list = ['R', 'G']
    elif char == 'R':
        left_list = ['B', 'G']
        right_list = ['G', 'B']
    for left, right in zip(left_list, right_list):
        left_idx = [j-i for i, x in enumerate(s[:j]) if x==left]
        right_idx = [i+1 for i, x in enumerate(s[j+1:]) if x==right]
        left_len = len(left_idx)
        right_len = len(right_idx)
        tmp = left_len * right_len
        tmp += len(set(left_idx) | set(right_idx)) - left_len - right_len
        res += tmp
print(res)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np
from numba import njit

MOD = 10**9 + 7

N = int(readline())
S = np.array(list(read().rstrip()), np.int8)

R = np.sum(S == ord('R'))
B = np.sum(S == ord('B'))
G = np.sum(S == ord('G'))

@njit
def f(S):
    N = len(S)
    ret = 0
    for i in range(N):
        for j in range(i + 1, N):
            k = j + j - i
            if k >= N:
                break
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                ret += 1
    return ret

answer = R * B * G - f(S)
print(answer)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
"""
i-j-k間が等しくなる組み合わせを求めてから、
Rの数*G*B - それ
で求まる？
"""
n = int(readline())
s = readline().rstrip().decode('utf-8')

if n == 3:
    print(0)
    exit()
dic1 = dict()
for i in s:
    if i in dic1:
        dic1[i] += 1
    else:
        dic1[i] = 1

if len(dic1) != 3:
    print(0)
    exit()
res = 1
for i in dic1.values():
    res *= i
#例外を求める
i = 0
x,y,z = 0,0,0
count = 0
while True:
    if i >= n:
        break
    j = 1
    while True:
        x = i
        y = i+j
        z = i+2*j
        if z >= n:
            break
        else:
            if s[x] != s[y] and s[x] != s[z] and s[y] != s[z]:
                count += 1 
        j += 1
        
    i += 1

print(res - count)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
n = int(input())
s = input()

color = 'RGB'
c_list = [[i for i in range(n) if s[i] == color[j]] for j in range(3)]
# print(*c_list, sep='\n')

ans1 = len(c_list[0]) * len(c_list[1]) * len(c_list[2])
ans2 = 0
for i in range(n):
    for j in range(1, n):
        if i + 2 * j >= n:
            continue
        if s[i] != s[i + j] and s[i + j] != s[i + 2 * j] and s[i +
                                                               2 * j] != s[i]:
            ans2 += 1

# print(ans1, ans2)
ans = ans1 - ans2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

sl = list(input())

sum_r = sl.count("R")
sum_g = sl.count("G")
sum_b = sl.count("B")

rl = []
gl = []
bl = []

for s in sl:
    if s == "R":
        sum_r -= 1
    if s == "G":
        sum_g -= 1
    if s == "B":
        sum_b -= 1
    rl.append(sum_r)
    gl.append(sum_g)
    bl.append(sum_b)
ans = 0
for i in range(n):
    for j in range(i+1, n):
        k = j + j - i
        if sl[i] == sl[j]:
            continue
        if "R" not in [sl[i], sl[j]]:
            ans += rl[j]
            if k < n:
                if sl[k] == "R":
                    ans -= 1
        if "G" not in [sl[i], sl[j]]:
            ans += gl[j]
            if k < n:
                if sl[k] == "G":
                    ans -= 1
        if "B" not in [sl[i], sl[j]]:
            ans += bl[j]
            if k < n:
                if sl[k] == "B":
                    ans -= 1
print(ans)

Submission status: Accepted

Solution code:

from collections import Counter, defaultdict
from math import gcd
import bisect

N = int(input())
S = input()

char_to_indices = defaultdict(list)
for i, c in enumerate(S):
    char_to_indices[c].append(i)

total = 0
for i in range(N):
    for j in range(i+1, N):
        if S[i] == S[j]:
            continue
        for c in ('G', 'B', 'R'):
            if c not in (S[i], S[j]):
                search = c
                break
        indices = char_to_indices[search]
        idx = bisect.bisect_left(indices, j+1)
        if idx != len(indices):
            total += len(indices) - idx
        exact_idx = bisect.bisect_left(indices, j + (j - i))
        if exact_idx != len(indices) and indices[exact_idx] == (2 * j - i):
            total -= 1
print(total)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right

N = int(input())
S = input()

R = []
G = []
B = []

res = 0

for i in range(N):
    if S[i] == 'R':
        R.append(i)
    elif S[i] == 'G':
        G.append(i)
    else:
        B.append(i)

for i in range(1, N - 1):
    if S[i] == 'R':
        res += bisect_left(G, i) * (len(B) - bisect_left(B, i))
        res += bisect_left(B, i) * (len(G) - bisect_left(G, i))
        for j in range(1, min(i, N - i - 1) + 1):
            if S[i - j] == 'G' and S[i + j] == 'B':
                res -= 1
            elif S[i - j] == 'B' and S[i + j] == 'G':
                res -= 1
    if S[i] == 'G':
        res += bisect_left(B, i) * (len(R) - bisect_left(R, i))
        res += bisect_left(R, i) * (len(B) - bisect_left(B, i))
        for j in range(1, min(i, N - i - 1) + 1):
            if S[i - j] == 'B' and S[i + j] == 'R':
                res -= 1
            elif S[i - j] == 'R' and S[i + j] == 'B':
                res -= 1
    if S[i] == 'B':
        res += bisect_left(R, i) * (len(G) - bisect_left(G, i))
        res += bisect_left(G, i) * (len(R) - bisect_left(R, i))
        for j in range(1, min(i, N - i - 1) + 1):
            if S[i - j] == 'G' and S[i + j] == 'R':
                res -= 1
            elif S[i - j] == 'R' and S[i + j] == 'G':
                res -= 1

print(res)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
s = input().rstrip()

c = Counter(list(s))

num_r = c['R']
num_g = c['G']
num_b = c['B']

bad = 0
for i in range(n-2):
    for j in range(i+1, n):
        k = j + (j - i)
        if k >= n:
            break

        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            bad += 1

print(num_r * num_g * num_b - bad)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
a = [0,0,0]
for c in s:
    if c=='R':
        a[0]+=1
    elif c=='G':
        a[1]+=1
    else:
        a[2]+=1
total = a[0]*a[1]*a[2]
for d in range(n):
    for i in range(n):
        j = i+d
        k = i+2*d
        if j>=n or k>=n:
            break
        if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
            total -= 1
print(total)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
from collections import Counter

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# １の条件を満たすものを調べたのち、２の条件のものを引く
N = ir()
S = list(sr())
counter = Counter(S)
r = counter['R']
g = counter['G']
b = counter['B']
answer = r * g * b
for i in range(N-1):
    for j in range(i+1, N):
        if j + (j - i) >= N:
            continue
        x = S[i]; y = S[j]; z = S[j+j-i]
        if x != y and y != z and z != x:
            answer -= 1

print(answer)

Submission status: Accepted

Solution code:
import sys
#input = sys.stdin.buffer.readline

def main():
    N = int(input())
    S = input()
    col = [[0 for _ in range(N+1)] for _ in range(3)]
    
    for i,st in enumerate(S):
        if st == "R":
            col[0][i+1] += 1
        elif st == "G":
            col[1][i+1] += 1
        else:
            col[2][i+1] += 1

    for i in range(3):
        for j in range(N):
            col[i][j+1] += col[i][j]
    
    ans = 0
    for i in range(N):
        for j in range(i+1,N):
            if S[i] == S[j]:
                continue
            elif (S[i] == "R" and S[j] == "G") or (S[j] == "R" and S[i] == "G"):
                ans += col[2][-1]-col[2][j]
                if N>j+(j-i) and S[j+(j-i)] == "B":
                    ans -= 1
            elif (S[i] == "G" and S[j] == "B") or (S[j] == "G" and S[i] == "B"):
                ans += col[0][-1]-col[0][j]
                if N>j+(j-i) and S[j+(j-i)] == "R":
                    ans -= 1
            else:
                ans += col[1][-1]-col[1][j]
                if N>j+(j-i) and S[j+(j-i)] == "G":
                    ans -= 1
    
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    n = int(input())
    s = input().strip()
    r = g = b = 0
    for c in s:
        if c == 'R':
            r += 1
        elif c == 'G':
            g += 1
        elif c == 'B':
            b += 1
    ans = r * b * g

    valid = frozenset('RGB')
    for i in range(n):
        for j in range(1, min(i, n-1-i)+1):
            if frozenset(s[i] + s[i-j] + s[i+j]) == valid:
                ans -= 1
    print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()

r=0
g=0
b=0


for ss in s:
    if ss == "R":
        r+=1
    elif ss == "G":
        g+=1
    elif ss == "B":
        b+=1
ng=0
i=0    
while i<n:
    j=1
    while i+2*j<n:
        if s[i]!=s[i+j] and s[i+j] != s[i+2*j] and s[i]!= s[i+2*j] :
            ng+=1
        j+=1
    i+=1
print(r*g*b-ng)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r = 0
g = 0
b = 0
ans = 0

for i in range(n):
    if s[i] == 'R':
        r += 1
    if s[i] == 'G':
        g += 1
    if s[i] == 'B':
        b += 1
rgb = {
    'R': r,
    'G': g,
    'B': b
}
for i in range(n):
    if s[i] != 'R':
        continue
    for j in range(n):
        if s[j] != 'G':
            continue
        mem = 0
        if (i+j)%2 == 0:
            k = (i+j)//2
            if s[k] == 'B':
                mem += 1
        if 2*i-j >= 0 and 2*i-j < n:
            if s[2*i-j] == 'B':
                mem += 1
        if 2*j-i >= 0 and 2*j-i < n:
            if s[2*j-i] == 'B':
                mem += 1
        ans += b-mem


print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
rl = 0
gl = 0
bl = 0
for i in range(n):
	if s[i] == 'R':
		rl += 1
	elif s[i] == 'G':
		gl += 1
	else:
		bl += 1
ans = rl*bl*gl
cnt = 0
for i in range(1,n//2+3):
	j = 0
	while j+i+i < n:
		if (s[j] == 'R'):
			if (s[j+i] == 'B'):
				if (s[j+i+i] == 'G'):
					cnt += 1
			elif (s[j+i] == 'G'):
				if (s[j+i+i] == 'B'):
					cnt += 1
		elif (s[j] == 'G'):
			if (s[j+i] == 'B'):
				if (s[j+i+i] == 'R'):
					cnt += 1
			elif (s[j+i] == 'R'):
				if (s[j+i+i] == 'B'):
					cnt += 1
		else:
			if (s[j+i] == 'R'):
				if (s[j+i+i] == 'G'):
					cnt += 1
			elif (s[j+i] == 'G'):
				if (s[j+i+i] == 'R'):
					cnt += 1
		j+=1
print(ans-cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = input()

r = 0
g = 0
b = 0

for i in range(n):
    if s[i] == "R":
        r += 1
    elif s[i] == "G":
        g += 1
    else:
        b += 1
ans = r * g * b

for i in range(n):
    for j in range(min(i, n - 1 - i)):
        if s[i]!=s[i+j+1] and s[i]!=s[i-j-1] and s[i+j+1]!=s[i-j-1]:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=[i for i in input()]
import collections
c=collections.Counter(s)
if len(c)<3:
    print(0)
    quit()
ans=c["R"]*c["G"]*c["B"]
for i in range(1,(n+1)//2):
    for j in range(n):
        if j+i+i>=n:
            break
        if s[j]!=s[j+i] and s[j]!=s[j+i+i] and s[j+i]!=s[j+i+i]:
            ans-=1
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    S = input()
    c = Counter(S)
    ans = c.get("R", 0) * c.get("G", 0) * c.get("B", 0)

    out = 0
    for start in range(N):
        for mid in range(start + 1, N):
            d = mid - start
            last = d + mid
            if last < N:
                if S[start] != S[mid] and S[mid] != S[last] and S[last] != S[start]:
                    out += 1

    print(ans - out)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N=int(input())
S=list(input())

count=[0]*3
for i in range(N-1,-1,-1):
    if S[i]=="R":
        count[0]+=1
    elif S[i]=="G":
        count[1]+=1
    else:
        count[2]+=1
        
cnt=0
for i in range(1,N//2+1):
    for j in range(N-2*i):
        a,b,c=S[j],S[j+i],S[j+2*i]
        if a!=b and a!=c and c!=b:
            cnt+=1

ans=count[0]*count[1]*count[2]-cnt
print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
s=input().rstrip()
r=[0]
g=[0]
b=[0]
for i in range(n-1,-1,-1):
    if s[i]=='R':
        r.append(r[-1]+1)
        g.append(g[-1])
        b.append(b[-1])
    elif s[i]=='G':
        r.append(r[-1])
        g.append(g[-1]+1)
        b.append(b[-1])
    elif s[i]=='B':
        r.append(r[-1])
        g.append(g[-1])
        b.append(b[-1]+1)
r.reverse()
g.reverse()
b.reverse()
ans=0
for i in range(n-2):
    for j in range(i+1,n-1):
        if s[i]==s[j]:
            continue
        if s[i]!='R' and s[j]!='R':
            d=j-i
            k=j+d
            ans+=r[j+1]
            if k<n:
                if s[k]=='R':
                    ans-=1
        elif s[i]!='G' and s[j]!='G':
            d=j-i
            k=j+d
            ans+=g[j+1]
            if k<n:
                if s[k]=='G':
                    ans-=1
        elif s[i]!='B' and s[j]!='B':
            d=j-i
            k=j+d
            ans+=b[j+1]
            if k<n:
                if s[k]=='B':
                    ans-=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

nR = S.count('R')
nG = S.count('G')
nB = S.count('B')

ans = nR*nG*nB

for x in range(1,N):
    for i in range(1,N):
        j = i+x
        k = i+2*x
        if k > N:
            break
        if S[i-1] != S[j-1] and S[j-1] != S[k-1] and S[k-1] != S[i-1]:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
rgb=[(0,0,0)]
for i in range(n):
	a=rgb[-1][0]
	b=rgb[-1][1]
	c=rgb[-1][2]
	if s[i]=="R":
		a+=1
	elif s[i]=="G":
		b+=1
	else:
		c+=1
	rgb.append((a,b,c))
ans=0
for i in range(n-2):
	for j in range(i+1,n-1):
		if s[i]!=s[j]:
			if "B" not in (s[i],s[j]):
				ans+=rgb[-1][2]-rgb[j+1][2]
				if 2*j-i<n:
					if s[2*j-i]=="B":
						ans-=1
			elif "G" not in (s[i],s[j]):
				ans+=rgb[-1][1]-rgb[j+1][1]
				if 2*j-i<n:
					if s[2*j-i]=="G":
						ans-=1
			else:
				ans+=rgb[-1][0]-rgb[j+1][0]
				if 2*j-i<n:
					if s[2*j-i]=="R":
						ans-=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

count = [0,0,0]

for c in S:
    if c == "R":
        count[0] += 1
    elif c == "G":
        count[1] += 1
    else:
        count[2] += 1

tot = count[0] * count[1] * count[2]

for i in range(N-2):
    max_n = (N-i-1)//2
    for j in range(1, max_n+1):
        if S[i]!= S[i+j] and S[i+j] != S[i+2*j] and S[i] != S[i+2*j]:
            #print(S[i], S[i+j], S[i+2*j])
            tot -= 1
print(tot)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    n = II()
    s = S()
    dpr = [0] * (n + 1)
    dpg = [0] * (n + 1)
    dpb = [0] * (n + 1)
    for i in range(n):
        dpr[i + 1] = dpr[i]
        dpg[i + 1] = dpg[i]
        dpb[i + 1] = dpb[i]
        if s[i] == "R":
            dpr[i+1] += 1
        elif s[i] == "G":
            dpg[i+1] += 1
        else:
            dpb[i + 1] += 1
    ans = 0

    for i in range(1, n - 1):
        if s[i] == "R":
            ans += dpg[i] * (dpb[-1] - dpb[i+1])
            ans += dpb[i] * (dpg[-1] - dpg[i+1])
        elif s[i] == "G":
            ans += dpb[i] * (dpr[-1] - dpr[i+1])
            ans += dpr[i] * (dpb[-1] - dpb[i+1])
        else:
            ans += dpg[i] * (dpr[-1] - dpr[i + 1])
            ans += dpr[i] * (dpg[-1] - dpg[i + 1])
    for j in range(1, n - 1):
        for i in range(1, j + 1):
            if j + i >= n:
                continue
            if s[j-i] != s[j] and s[j] != s[j + i] and s[j-i] != s[j + i]:
                ans -= 1
    print(ans)
                
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from collections import Counter
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n=II()
    s=SI()
    cc=Counter(s)
    if len(cc)<3:
        print(0)
        exit()
    tot=1
    for c in cc.values():tot*=c
    for i,c in enumerate(s):
        for j in range(1,min(i,n-1-i)+1):
            if s[i-j]!=c and c!=s[i+j] and s[i-j]!=s[i+j]:
                tot-=1
    print(tot)

main()