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:
# D - RGB Triplets
from collections import defaultdict


def main():
    """
    character  A ---- B ---- C
    index     X-dx    X     X+dx
        -> A_index + C_index == 2 * B_index
    """
    _, S = open(0).read().split()
    indices = defaultdict(set)
    for i, c in enumerate(S):
        indices[c].add(i)

    cnt = len(indices["R"]) * len(indices["G"]) * len(indices["B"])
    for x, y, z in ("RGB", "RBG", "BGR"):
        cnt -= sum(
            (i + j) % 2 == 0 and (i + j) // 2 in indices[z]
            for i in indices[x]
            for j in indices[y]
        )
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# D - RGB Triplets
from collections import defaultdict


def main():
    _, S = open(0).read().split()
    indices, doubled_indices = defaultdict(list), defaultdict(set)
    for i, c in enumerate(S):
        indices[c].append(i)
        doubled_indices[c].add(2 * i)

    cnt = len(indices["R"]) * len(indices["G"]) * len(indices["B"])
    for x, y, z in ("RGB", "RBG", "BGR"):
        cnt -= sum(i + j in doubled_indices[z] for i in indices[x] for j in indices[y])
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 11 17:23:36 2020

@author: Aruto Hosaka
"""

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


color = ['R','G','B']
k = 1

A = {color[0]:0,color[1]:0,color[2]:0}

for c1 in color:
    for c2 in color:
        for c3 in color:
            if c1 != c2 and c2 != c3 and c1 != c3:
                A[(c1,c2,c3)] = 0

for c1 in color:
    for c2 in color:
        if c1!=c2:
            A[(c1,c2)] = 0

for i in range(N):
    if S[i] == 'R':
        A[('G','B','R')] += A[('G','B')]
        A[('B','G','R')] += A[('B','G')]
        A[('B','R')] += A['B']
        A[('G','R')] += A['G']
    if S[i] == 'G':
        A[('R','B','G')] += A[('R','B')]
        A[('B','R','G')] += A[('B','R')]
        A[('B','G')] += A['B']
        A[('R','G')] += A['R']
    if S[i] == 'B':
        A[('G','R','B')] += A[('G','R')]
        A[('R','G','B')] += A[('R','G')]
        A[('R','B')] += A['R']
        A[('G','B')] += A['G']
    A[S[i]] += 1

counter = 0
for c1 in color:
    for c2 in color:
        for c3 in color:
            if c1 != c2 and c2 != c3 and c1 != c3:
                counter += A[(c1,c2,c3)]
                
dc = 0
for i in range(N-2):
    k = 1
    while k*2+i < N:
        if S[i+k] != S[i] and S[i] != S[i+k*2] and S[i+k] != S[i+k*2]:
            dc += 1
        k += 1
print(counter-dc)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cnt = s.count('R') * s.count('G') * s.count('B')
for i in range(n - 1):
  for j in range(i + 1, n):
    if 2 * j - i <= n - 1 and len(set([s[i], s[j], s[2 * j - i]])) == 3:
      cnt -= 1
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = list(input())
cnt = Counter(S)
ans = 1
for s in list('RGB'):
    ans *= cnt[s]

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
R = set()
G = set()
B = set()
for i, s in enumerate(S):
    if s == 'R':
        R.add(i)
    elif s == 'G':
        G.add(i)
    elif s == 'B':
        B.add(i)

ans = 0
for r in R:
    for g in G:
        ans += len(B)
        d = abs(r-g)
        if (r+g) % 2 == 0 and (r+g)//2 in B:
            ans -= 1
        if r+d in B or g+d in B:
            ans -= 1
        if r-d in B or g-d in B:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())

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

ans = r*g*b
count  = 0
sub1 = 0

for i in range(n):
    for j in range(i+1,n):
        if 2*j-i >= n or 2*j-i <0:
            break
        elif c[2*j-i] != c[i] and c[i] != c[j] and c[2*j-i] != c[j]:
                sub1 += 1


print(ans-sub1)

Submission status: Accepted

Solution code:
from collections import defaultdict
 
 
def main():
    _, S = open(0).read().split()
    indices, doubled_indices = defaultdict(list), defaultdict(set)
    for i, c in enumerate(S):
        indices[c].append(i)
        doubled_indices[c].add(2 * i)
    cnt = S.count("R") * S.count("G") * S.count("B")
    for x, y, z in ["RGB", "GBR", "BRG"]:
        cnt -= sum(i + j in doubled_indices[z] for i in indices[x] for j in indices[y])
    print(cnt)
 
 
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    import re
    n = int(input())
    s = input()
    rindex = set()
    gindex = set()
    bindex = set()
    for i in range(n):
        if s[i] == "R":
            rindex.add(i)
        elif s[i] == "G":
            gindex.add(i)
        else:
            bindex.add(i)
    ans = len(rindex) * len(gindex) * len(bindex)

    for r in rindex:
        for g in gindex:
            if r > g:
                gap = r - g
                b1 = r + gap
                b2 = g - gap
                b3 = g + (gap/2)

            elif r < g:
                gap = g - r
                b1 = g + gap
                b2 = r - gap
                b3 = r + (gap/2)
            if b1 in bindex:
                ans -= 1
            if b2 in bindex:
                ans -= 1  
            if b3 in bindex:
                ans -= 1  
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

def I(): 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 main():
    n = I()
    s = list(input())
    r = s.count('R')
    g = s.count('G')
    b = s.count('B')
    cnt = r*g*b
    for i in range(n):
        for j in range(i+1, n):
            k = 2*j - i
            if k >= n:
                break
            if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                cnt -= 1
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
l = [1] * N
i = 0
d = {1:0,2:0,3:0}
for s in input():
    n = 1
    if s == "G":
        n = 2
    elif s == "B":
        n = 3
    l[i] = n
    d[n] += 1
    i += 1
cnt = d[1] * d[2] * d[3]
for j in range(1,(N - 1) // 2 + 1):
    for k in range(N - (2 * j)):
        if l[k] * l[k+j] * l[k+j+j] == 6:
            cnt -= 1
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter

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

counter = Counter(S)

r = 1
for c in "RGB":
    r *= counter[c]


for i in range(N):
    for j in range(i+1, N):
        k = 2*j-i
        # print(i, j, k)
        if k < N and len({S[i], S[j], S[k]}) == 3:
            r -= 1

print(r)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def main():
    n= int(input())
    s = str(input().rstrip())
    r= s.count('R')
    g=s.count('G')
    b = s.count('B')
    ans = 0
    for i in range(n-2):
        for j in range(i+1, (n+i+1)//2):
            k = 2*j-i
            t = {s[i], s[j], s[k]}
            if len(t) ==3:
                ans +=1
    print(r*g*b-ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from operator import mul
from functools import reduce
from collections import Counter

n = int(input())
s = input()

c = Counter(s)
if len(c) != 3:
    print(0)
    sys.exit()

ans = reduce(mul, c.values())

for i in range(n - 1):
    x, y, z = i, i + 1, i + 2
    while z <= n - 1:
        if s[x] != s[y] and s[x] != s[z] and s[y] != s[z]:
            ans -= 1
        y += 1
        z += 2

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


n = int(input())
s = input()
count = Counter(s)

ans = count["R"] * count["G"] * count["B"]
for i in range(n):
    for j in range(i+1, n):
        k = 2*j - i
        if k > n - 1:
            break
        if s[i] != s[j] != s[k] != s[i]:
            ans -= 1

print(ans)

Submission status: Accepted

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

l = list(S)
ans = l.count('R')*l.count('G')*l.count('B')
for i in range(N-2):
  a = l[i]
  for j in range(1, (N+1-i)//2):
    b, c = l[i+j], l[i+2*j]
    if (a!=b) and (b!=c) and (c!=a):
      ans -= 1
        
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    n = int(input())
    s = input()
    cnt = Counter(s)
    ans = cnt["R"] * cnt["G"] * cnt["B"]
    for gap in range(1, (n - 1) // 2 + 1):
        for i in range(n - gap * 2):
            if set([s[i], s[i + gap], s[i + 2 * gap]]) == {"R", "G", "B"}:
                ans -= 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

fullcount= s.count('R')*s.count('G')*s.count('B')

for i in range(n-2):
    for j in range(i+1,n):
        k = 2*j-i
        if k > (n-1):
            break
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            count += 1
ans = fullcount - count
print(ans)

Submission status: Accepted

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

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


cntR = cntG = cntB = 0
for i in range(N):
    if S[i] == 'R':
        cntR += 1
    elif S[i] == 'G':
        cntG += 1
    else:
        cntB += 1

ans = cntR*cntG*cntB - count
print(ans)

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 i in range(n-2):
	for j in range(i+1,n-1):
		k=j+(j-i)
		if k>=n:
			break
		if s[i]!=s[j]!=s[k]!=s[i]:
			ans -=1
print(ans)

Submission status: Accepted

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

tot = s.count('R') * s.count('G') * s.count('B')
#print(tot)

for i in range(n):
    for d in range(1,n):
        j = i+d
        k = j+d
        if k > n-1:
            break
        if s[i]!=s[j]!=s[k]!=s[i]:
            tot -= 1

print(tot)

Submission status: Accepted

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

tot = s.count('R') * s.count('G') * s.count('B')
#print(tot)

for i in range(n):
    for d in range(1,n):
        j = i+d
        k = j+d
        if k > n-1:
            break
        if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
            tot -= 1

print(tot)

Submission status: Accepted

Solution code:
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
S = readline().decode().rstrip()
R_cnt = S.count('R')
G_cnt = S.count('G')
B_cnt = S.count('B')
ans1 = R_cnt * G_cnt * B_cnt
cnt  = 0
d_max = N // 2 +1
for d in range(1,d_max):
    for i in range(N):
        if i+2*d >= N:
            break
        if (S[i] != S[i+d]) and (S[i+d] != S[i+2*d]) and (S[i] !=S[i+2*d]):
           cnt += 1
ans = ans1 - cnt
print(ans)

Submission status: Accepted

Solution code:
import collections
n=int(input())
S=input()
c=collections.Counter(S)
ans=c['R']*c['B']*c['G']
hiku=0
for i in range(n):
    for d in range(n):
        j=i+d
        k=j+d
        if k>n-1:
            break
        if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
          hiku+=1
print(ans-hiku)

Submission status: Accepted

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


# 持っているビスケットを叩き、1枚増やす
# ビスケット A枚を 1円に交換する
# 1円をビスケット B枚に交換する
def main():
    n = int(input())
    s = input()
    R, G, B = [], [], []
    for i in range(n):
        index = i+1
        if s[i] == "R":
            R.append(index)
        elif s[i] == "G":
            G.append(index)
        elif s[i] == "B":
            B.append(index)
    all_count = len(R)*len(G)*len(B)
    count = 0
    # j - i != k - j
    # k = 2j - i
    for i in range(n):
        for j in range(i+1, n):
            if s[i] == s[j]:
                continue
            k = 2*j - i
            if k >= n:
                continue
            if s[k] == s[i] or s[k] == s[j]:
                continue
            count += 1
    print(all_count - count)

def bi(num, a, b, x):
    print((a * num) + (b * len(str(b))))
    if (a * num) + (b * len(str(b))) <= x:
        return False
    return True

def input_list():
    return list(map(int, input().split()))


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ANSHUL GAUTAM
# IIIT-D

from math import *
from copy import *					# ll = deepcopy(l)
from heapq import *					# heappush(hp,x)
from string import *				# alpha = ascii_lowercase
from random import *				# l.sort(key=lambda l1:l1[0]-l1[1]) => ex: sort on the basis difference
from bisect import *				# bisect_left(arr,x,start,end)  => start and end parameters are temporary
from sys import stdin				# bisect_left return leftmost position where x should be inserted to keep sorted
from sys import maxsize				# minn = -maxsize
from operator import *				# d = sorted(d.items(), key=itemgetter(1))
from itertools import *				# pre = [0] + list(accumulate(l))
from decimal import Decimal 		# a = Decimal(a)	# use this for math questions
from collections import Counter		# d = dict(Counter(l))
from collections import defaultdict # d = defaultdict(list)

'''
4
RGRBG

[0,2]
[1,4]
[3,5]
'''

def solve(s):
	n = len(s)
	r,g,b = s.count('R'),s.count('G'),s.count('B')
	sub = 0
	for i in range(n):
		for j in range(i+1,n):
			k = 2*j - i
			if(k >= n):
				break
			if(len(set([s[i],s[j],s[k]])) == 3):
				sub += 1
	return r*g*b - sub


N = int(stdin.readline())
l = input()
ans = solve(l)
print(ans)

Submission status: Accepted

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

color_cnt = {'R': 0, 'G': 0, 'B': 0}
for i in range(N):
    color_cnt[S[i]] += 1

ans = color_cnt['R']*color_cnt['G']*color_cnt['B']
for i in range(N):
    for diff in range(1, (N-i-1)//2 +1):
        ans -= len({S[i], S[i+diff], S[i+2*diff]}) == 3

print(ans)

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:
n = int(input())
s = input()
R = s.count("R")
G = s.count("G")
B = s.count("B")

total = R*G*B

for i in range(n-1):
    for j in range(i+1,n):
        if j*2-i < n:
            if s[i] != s[j] and s[j] != s[j*2-i] and s[i] != s[j*2-i]:
                total -= 1
        else:
            continue

print(total)

Submission status: Accepted

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

for i in range(N-2):
    if S[i] == "R":
        num_G = S[i+1:].count('G')
        num_B = S[i+1:].count('B')
        total += num_G * num_B
    elif S[i] == "G":
        num_B = S[i+1:].count('B')
        num_R = S[i+1:].count('R')
        total += num_B * num_R
    elif S[i] == "B":
        num_G = S[i+1:].count('G')
        num_R = S[i+1:].count('R')
        total += num_G * num_R

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

print(total)

Submission status: Accepted

Solution code:
#162D
#RGB Triplets

import collections

n=int(input())
S=input()
s=collections.Counter(S)
ans=s['R']*s['G']*s['B']
for i in range(n):
    for d in range(n):
        j=i+d
        k=j+d
        if k>n-1:
            break
        if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
            ans-=1
print(ans)

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(0,N-2) :
    for j in range(i+1,N-1) :
        k = j + (j - i)
        if k < N :
            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 = 'X' + input()

cnt_R, cnt_G, cnt_B = 0, 0, 0
for s in S[1:]:
    if s == 'R':
        cnt_R += 1
    elif s == 'G':
        cnt_G += 1
    elif s == 'B':
        cnt_B += 1

ans = cnt_R * cnt_G * cnt_B

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r = list(s).count('R')
g = list(s).count('G')
b = list(s).count('B')

ans = r*g*b
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[k]!=s[i]):
            ans -=1
print(ans)

Submission status: Accepted

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

r=list(s).count('R')
g=list(s).count('G')
b=list(s).count('B')

ans=r*g*b
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[k]!=s[i]):
#            print(i,j,k)
            ans-=1
print(ans)

Submission status: Accepted

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

# 条件①はrgbの組み合わせが"R","G","B"がそれぞれ選ばれる場合のみを想定
# それぞれの出現回数をかけ合わせることで全組み合わせを網羅できる。
r = s.count("R")
g = s.count("G")
b = s.count("B")
rgb = r*g*b

# 条件②：j-i ≠ k-j を別の見方をすると k = 2*j-i になる。
# rgbの全通りの組み合わせから上記の組み合わせになる回数を引く
# ただし、条件①に該当する組み合わせについてはすでに引かれている為、スキップする。
for i in range(n-2):
  for j in range(i+1, n-1):
    k = 2*j-i 
    if k >= n or s[i] == s[j] or s[i] == s[k] or s[j] == s[k]:
      continue
    rgb -= 1
print(rgb)

Submission status: Accepted

Solution code:
N = int(input())
S = list(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

allcnt = r * g * b

sub = 0
for i in range(N):
    for j in range(i+1,N):
        if S[i] == S[j]:
            None
        else:
            k = 2*j-i
            if k >= N or S[i] == S[k] or S[j] == S[k]:
                None
            else:
                sub += 1

print(allcnt - sub)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
s = input()
cnt = Counter(s)

ans = cnt['R'] * cnt['G'] * cnt['B']
for i in range(n):
    for j in range(n):
        k = 2 * j - i
        if k > n-1:
            break
        if i < j < k and s[i] != s[j] and s[j] != s[k] and s[i] != s[k]:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby
from statistics import median
from sys import exit

N = int(input())

line = ([(index, v) for index, v in enumerate(input())])


def types(line): return line[1]
def index(line): return line[0]


gp = groupby(sorted(line, key=types), types)

d = {}

for k, v in gp:
    indexes = set([i for i, value in v])
    d[k] = indexes

if 'R' not in d or 'G' not in d or 'B' not in d:
    print(0)
    exit()

delete = 0


for differ in range(1, N):
    for start in range(N):
        if start + differ*2 > N - 1:
            break

        i = line[start]
        j = line[start + differ]
        k = line[start + differ*2]

        if len({i[1], j[1], k[1]}) != 3:
            continue

        upper = max(i[0], j[0], k[0])
        lower = min(i[0], j[0], k[0])
        median_num = median([i[0], j[0], k[0]])
        if upper - median_num == median_num - lower:
            delete += 1

print(len(d['R']) * len(d['G']) * len(d['B']) - delete)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
count_R = 0
count_G = 0
count_B = 0
count = 0
for i in range(N):
    if S[i] == 'R':
        count_R = count_R+1
    elif S[i] == 'G':
        count_G = count_G+1
    else:
        count_B = count_B+1
count = count_R*count_G*count_B

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

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(N):
    for j in range(i + 1, N):
        k = 2 * j - i
        if k < N:
            if sorted(S[i] + S[j] + S[k]) == ['B', 'G', 'R']:
                ans -= 1
                
print(ans)

Submission status: Accepted

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

r = [0 for _ in range(N + 1)]
g = [0 for _ in range(N + 1)]
b = [0 for _ in range(N + 1)]

for i in reversed(range(N)):
    r[i] = r[i + 1]
    g[i] = g[i + 1]
    b[i] = b[i + 1]
    if S[i] == "R":
        r[i] += 1
    if S[i] == "G":
        g[i] += 1
    if S[i] == "B":
        b[i] += 1
        
ans = 0

for i in range(N):
    if S[i] == "R":
        for j in range(i + 1, N):
            if S[j] == "G":
                if 2 * j - i < N:
                    if S[2 * j - i] == "B":
                        ans += (b[j + 1] - 1)
                    else:
                        ans += b[j + 1]
                else:
                    ans += b[j + 1]
                    
for i in range(N):
    if S[i] == "R":
        for j in range(i + 1, N):
            if S[j] == "B":
                if 2 * j - i < N:
                    if S[2 * j - i] == "G":
                        ans += (g[j + 1] - 1)
                    else:
                        ans += g[j + 1]
                else:
                    ans += g[j + 1]
                    
for i in range(N):
    if S[i] == "B":
        for j in range(i + 1, N):
            if S[j] == "R":
                if 2 * j - i < N:
                    if S[2 * j - i] == "G":
                        ans += (g[j + 1] - 1)
                    else:
                        ans += g[j + 1]
                else:
                    ans += g[j + 1]
                    
for i in range(N):
    if S[i] == "B":
        for j in range(i + 1, N):
            if S[j] == "G":
                if 2 * j - i < N:
                    if S[2 * j - i] == "R":
                        ans += (r[j + 1] - 1)
                    else:
                        ans += r[j + 1]
                else:
                    ans += r[j + 1]
                    
for i in range(N):
    if S[i] == "G":
        for j in range(i + 1, N):
            if S[j] == "R":
                if 2 * j - i < N:
                    if S[2 * j - i] == "B":
                        ans += (b[j + 1] - 1)
                    else:
                        ans += b[j + 1]
                else:
                    ans += b[j + 1]
                    
for i in range(N):
    if S[i] == "G":
        for j in range(i + 1, N):
            if S[j] == "B":
                if 2 * j - i < N:
                    if S[2 * j - i] == "R":
                        ans += (r[j + 1] - 1)
                    else:
                        ans += r[j + 1]
                else:
                    ans += r[j + 1]
                    
print(ans)

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(len(s)):
    for j in range(i+1,len(s)):
        if s[i]!=s[j]:
            k=j+j-i
            if k>=len(s):
                break
            else:
                if s[i]!=s[k] and s[j]!=s[k]:
                    ans-=1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = input()
cnt = Counter(s)
ans = cnt["R"]*cnt["G"]*cnt["B"]
for i in range(n):
    for j in range(i+1,n):
        k = 2*j-i
        if 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:
import sys
from collections import defaultdict
N = int(input())
if N <= 3:
    print(0)
    sys.exit()
s = input()
ls = [defaultdict(int) for i in range(N)]
a = ['R', 'G', 'B']
for i, x in enumerate(s):
    for y in a:
        if x == y:
            ls[i][y] = ls[max(0, i-1)][y] + 1
        else:
            ls[i][y] = ls[max(0, i-1)][y]
ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        if s[i] != s[j]:
            for k in a:
                if k != s[i] and k != s[j]:
                    ans += ls[-1][k] - ls[j][k]
            if 2*j-i <N and s[i] != s[2*j-i] and s[j] != s[2*j-i]:
                ans -= 1
print(ans)

Submission status: Accepted

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

A = [0]*3

for s in S:
    if s == "R":
        A[0] += 1
    elif s =="G":
        A[1] += 1
    elif s == "B":
        A[2] += 1
        
ans = 1

for a in A:
    ans *= a

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

Submission status: Accepted

Solution code:
from collections import Counter

def SUMCHK():
    N = int(input())
    S = input()

    C=Counter(S)
    r = C['R']
    g = C['G']
    b = C['B']


    cnt = r * g * b

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

    print(cnt)

SUMCHK()

Submission status: Accepted

Solution code:
import itertools
from collections import Counter

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

C=Counter(S)
r = C['R']
g = C['G']
b = C['B']


cnt = r * g * b

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

print(cnt)

Submission status: Accepted

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

R = []
G = []
list = [0]*n
number_B = 0
for i in range(n):
  if s[i] == 'R':
    R.append(i)
  elif s[i] == 'G':
    G.append(i)
  else:
    list[i] = 1
    number_B += 1
    

ans = 0
for i in R:
  for j in G:
    minus = 0
    if i < j:
      if 2*i-j >= 0 and list[2*i-j] == 1:
        minus += 1   
      if (i+j)%2 == 0 and list[int((i+j)/2)] == 1:
        minus += 1      
      if 2*j-i<n and list[2*j-i] == 1:
        minus += 1
    else:
      if 2*j-i >= 0 and list[2*j-i] == 1:
        minus += 1   
      if (i+j)%2 == 0 and list[int((i+j)/2)] == 1:
        minus += 1      
      if 2*i-j<n and list[2*i-j] == 1:
        minus += 1
    ans += (number_B-minus)
    
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
S=input()
C=Counter(S)
nr=C["R"]
ng=C["G"]
nb=C["B"]
ans=nr*ng*nb
for i in range(N-2):
    for j in range(i+1,N-1):
        k=j+j-i
        if k<N:
            if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                ans-=1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

dic = Counter(S)
cnt = 0

for i in range(0,N-2):
  for j in range(i+1,N-1):
    if S[i] == S[j]:
      continue
    elif 2*j - i < N:
      if S[i] != S[2*j-i] and S[j] != S[2*j-i]:
        cnt += 1
        
print(dic["R"]*dic["B"]*dic["G"]-cnt)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
R = [0]*N
G = [0]*N
B = [0]*N
for i in range(N):
    if S[i]=="R":
        R[i] += 1
    elif S[i] == "G":
        G[i] += 1
    else:
        B[i] += 1

ans = sum(R)*sum(G)*sum(B)

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

Submission status: Accepted

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

r=[[0,0,0] for _ in range(n)]
c=0
l={"GB":0,"BG":0,"RB":1,"BR":1,"RG":2,"GR":2}
for i in range(n):
  if s[i] =="R":
    r[i][0]=r[i-1][0]+1
    r[i][1]=r[i-1][1]
    r[i][2]=r[i-1][2]
  elif s[i]=="G":
    r[i][1]=r[i-1][1]+1
    r[i][0]=r[i-1][0]
    r[i][2]=r[i-1][2]
  elif s[i]=="B":
    r[i][2]=r[i-1][2]+1
    r[i][0]=r[i-1][0]
    r[i][1]=r[i-1][1]

for i in range(n-2):
  for j in range(i+1,n-1):
    if s[i]==s[j]:
      continue
    else:
      c+=r[n-1][l[s[i]+s[j]]]-r[j][l[s[i]+s[j]]]
     # print("p",i,j,c)
      
      if 2*j-i<n:
        if s[2*j-i]!= s[i] and s[2*j-i]!=s[j]:
          c-=1
      #   print("m",i,j,c)
  
print(c)

Submission status: Accepted

Solution code:
def readinput():
    n=int(input())
    s=input()
    s=' '+s
    return n,s

def main(n,s):
    count=0
    all=set(['R','G','B'])
    for i in range(1, n-1):
        l1=s[i]
        for j in range(i+1, n):
            l2=s[j]
            if l1==l2:
                continue
            l3=(all - set([l1,l2])).pop()
            ij=j-i
            sub1=s[j+1:j+ij]
            sub2=s[j+ij+1:]
            count+=sub1.count(l3)
            count+=sub2.count(l3)
    return count

def main2(n,s):
    count=0
    nr=s.count('R')
    ng=s.count('G')
    nb=s.count('B')
    all=set(['R','G','B'])
    for i in range(1, n-1):
        l1=s[i]
        for j in range(i+1, n):
            l2=s[j]
            if l1==l2:
                continue
            ij=j-i
            jk=ij
            k=j+jk
            if(k>n):
                break
            l3=s[k]
            if l3==l1 or l3==l2:
                continue
            count+=1
    return nr*ng*nb-count



if __name__=='__main__':
    n,s=readinput()
    ans=main2(n,s)
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
rgb = [0]*3

for i in range(N):
    if S[i] == "R":
        rgb[0] += 1
    elif S[i] == "G":
        rgb[1] += 1
    else:
        rgb[2] += 1
ans = 0
for i in range(N):
    for h in range(N):
        j = i+h
        k = i+2*h
        if k >= N:
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            ans += 1

print(rgb[0]*rgb[1]*rgb[2]-ans)

Submission status: Accepted

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

ans = s.count("R") * s.count("B") * s.count("G")

for i in range(n):
    if lis[i] == "R":
        lis[i] = 1
    elif lis[i] == "G":
        lis[i] = 2
    else:
        lis[i] = 4

for i in range(n):
    for j in range(i+1, n):
        k = 2*j -i
        if k >= n:
            continue
        if lis[i] + lis[j] + lis[k] == 7:
            ans -= 1
        
print(ans)