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:
N = int(input())
S = input()
ans = S.count('R')*S.count('G')*S.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 and S[i]!=S[k] and S[j]!=S[k]:
                ans -= 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
R = set()
G = set()
B = 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(R,G,B)
x = 0
for i in R:
  for j in G:
    if (i - j) % 2 == 0 and (i + j) // 2 in B:
      x -= 1
    if 2 * j - i in B:
      x -= 1
    if 2 * i - j in B:
      x -= 1
    # print(i,j,x)
x += len(R) * len(B) * len(G)
print(x)

Submission status: Accepted

Solution code:
import sys
import numpy as np

# S = input()
# N, M = map(int, input().split())
# As = [int(input()) for _ in range(N)]
# nums = list(map(int, input().split()))
# nums_2d = [list(map(int, input().split())) for _ in range(N)]


def main():
    solve()


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

    ridxs = []
    gidxs = []
    bidxs = []

    rdic = dict()
    gdic = dict()
    bdic = dict()

    for i in range(N):
        s = S[i]
        if s == 'R':
            ridxs.append(i)
            rdic[i*2] = 1
        if s == 'G':
            gidxs.append(i)
            gdic[i*2] = 1
        if s == 'B':
            bidxs.append(i)
            bdic[i*2] = 1

    total = len(ridxs) * len(gidxs) * len(bidxs)

    for ri in ridxs:
        for gi in gidxs:
            if ri + gi in bdic:
                total -= 1

    for gi in gidxs:
        for bi in bidxs:
            if gi + bi in rdic:
                total -= 1

    for bi in bidxs:
        for ri in ridxs:
            if bi + ri in gdic:
                total -= 1

    print(total)
    return


if __name__ == '__main__':
    main()

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 n <= k:
            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()

total = 0
m = 0

for j in range(1, N):

    ic = {
        "R": 0,
        "G": 0,
        "B": 0
    }

    kc = {
        "R": 0,
        "G": 0,
        "B": 0
    }

    m = 0

    for i in range(j-1, -1, -1):
        ic[S[i]] += 1
    
    for k in range(j+1, N):
        kc[S[k]] += 1

    for l in range(min(j+1, N-j)):
        if(S[j - l] != S[j + l] and S[j] != S[j + l] and S[j] != S[j - l]):
            m += 1

    if(S[j] == "R"):
        total += ic["G"] * kc["B"] + ic["B"] * kc["G"]
    elif(S[j] == "G"):
        total += ic["R"] * kc["B"] + ic["B"] * kc["R"]
    elif(S[j] == "B"):
        total += ic["R"] * kc["G"] + ic["G"] * kc["R"]

    total -= m

print(total)

Submission status: Accepted

Solution code:
from collections import Counter
def main():
    n = int(input())
    s = input()
    d = Counter(s)
    ans = d['R'] * d['G'] * d['B']

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

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
SL = list(S)
cnt = 0
nr = SL.count('R')
ng = SL.count('G')
nb = SL.count('B')
ans = nr * ng * nb
cnt = 0
for i in range(len(S)):
    for d in range(N):
        j = i + d
        k = j + d
        if k >= N:
            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()
ls = list(s)
all = ls.count('R')* ls.count('G')* ls.count('B')

cnt = 0

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

print(all - cnt)

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(1,int((n-i)/2)+1):
        if i + j + j <= n-1:
            if s[i] != s[i+j] and s[i] != s[i+j+j] and s[i+j] != s[i+j+j] :
                ans = ans - 1

print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import collections
input = sys.stdin.readline

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

cnt = collections.Counter(S)
ans = cnt['R'] * cnt['G'] * cnt['B']

for i in range(N):
	for d in range(1,N):
		j, k = i+d, i+2*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()

cR = len([1 for s in S if s == 'R'])
cG = len([1 for s in S if s == 'G'])
cB = len([1 for s in S if s == 'B'])
# 条件2を無視したときの組の数
_all = cR * cG * cB

# 条件2でNGになるような組の数
ng_condition2 = 0
for i in range(N-1):
  for j in range(i+1, N):
    k = j+j-i
    if k >= N:
      break
    if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
      ng_condition2 += 1

print(_all - ng_condition2)

Submission status: Accepted

Solution code:
N=int(input())
K=input()
dic = {}
ts = 'RGB'
for t in ts:
    p=[]
    for ir, r in enumerate(K):
        if r == t:
            p.append(ir)
    dic[t] = p
ans1 = len(dic['R']) * len(dic['G']) * len(dic['B'])

ans2 = 0

for i in range(N):
    for j in range(i, N):
        if (i + 2*(j-i) < N):
            if  K[i] != K[j] and K[i] != K[i + 2*(j-i)] and K[i+2*(j-i)] != K[j]:
                ans2 +=1

print(ans1 - ans2)

Submission status: Accepted

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

r = 0
g = 0
b = 0

for x in s:
    if x == 'R':
        r += 1
    elif x == 'G':
        g += 1
    else:
        b += 1

ans = r*g*b

for i in range(n):
    for j in range(i+1, n):
        if 2*j-i < n and len(set([s[i], s[j], s[2*j-i]])) == 3:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
s_list = list(s)
ans = 0
total = s_list.count("R") * s_list.count("G") * s_list.count("B")
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        k = j + j - i
        if n >= k + 1:
            if (s_list[i] != s_list[j]) and (s_list[j] != s_list[k]) and (s_list[k] != s_list[i]):
                ans += 1
print(total - ans)

Submission status: Accepted

Solution code:
def rest_col(s1,s2):
  if(s1=="R"):
    if(s2=="R"):
      return -1
    elif(s2=="G"):
       return 2
    else:
       return 1
  if(s1=="G"):
    if(s2=="G"):
      return -1
    elif(s2=="R"):
       return 2
    else:
       return 0
  if(s1=="B"):
    if(s2=="B"):
      return -1
    elif(s2=="R"):
       return 1
    else:
       return 0

str_c=["R","G","B"]
n=int(input())
s=input()
count=[[0,0,0] for i in range(n)] #count[i][c]はi番目以上で色cの個数
if(s[n-1]=="R"):
  count[n-1][0]=1
if(s[n-1]=="G"):
  count[n-1][1]=1
if(s[n-1]=="B"):
  count[n-1][2]=1
for i in range(1,n):
  count[n-1-i][0]=count[n-i][0]
  count[n-1-i][1]=count[n-i][1]
  count[n-1-i][2]=count[n-i][2]
  if(s[n-1-i]=="R"):
    count[n-1-i][0]+=1
  if(s[n-1-i]=="G"):
    count[n-1-i][1]+=1
  if(s[n-1-i]=="B"):
    count[n-1-i][2]+=1
ans=0
for a in range(n-1):
  for b in range(a,n-1):
    r=rest_col(s[a],s[b])
    if(r!=-1):
      ans+=count[b+1][r]
      if(2*b-a<len(s)):
        if(s[2*b-a]==str_c[r]):
          ans-=1
print(ans)

Submission status: Accepted

Solution code:
n,s=open(0);n=int(n);c=s.count;print(c('R')*c('G')*c('B')-sum(len(set(s[i:j+1:j-i>>1]))>2for i in range(n)for j in range(i+2,n,2)))

Submission status: Accepted

Solution code:
N = int(input())

S = input()

ans = 0


sumR = [0]
sumG = [0]
sumB = [0]

for s in S:
    if s == "R":
        sumR.append(sumR[-1] + 1)
        sumG.append(sumG[-1])
        sumB.append(sumB[-1])
    elif s == "G":
        sumR.append(sumR[-1])
        sumG.append(sumG[-1] + 1)
        sumB.append(sumB[-1])
    elif s == "B":
        sumR.append(sumR[-1])
        sumG.append(sumG[-1])
        sumB.append(sumB[-1] + 1)


for i, s in enumerate(S):
    not_cnt = 0
    for k in range(1, min(i + 1, N - i)):
        L, R = S[i - k], S[i + k]
        if s != L and s != R and L != R:
            not_cnt += 1

    cnt = 0
    if s == "R":
        cnt += sumG[i] * (sumB[-1] - sumB[i + 1])
        cnt += sumB[i] * (sumG[-1] - sumG[i + 1])
    elif s == "G":
        cnt += sumR[i] * (sumB[-1] - sumB[i + 1])
        cnt += sumB[i] * (sumR[-1] - sumR[i + 1])
    elif s == "B":
        cnt += sumG[i] * (sumR[-1] - sumR[i + 1])
        cnt += sumR[i] * (sumG[-1] - sumG[i + 1])

    cnt -= not_cnt
    ans += cnt


print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
rcount = 0
gcount = 0
bcount = 0
for i in range(N):
  if S[i] == 'R':
    rcount += 1
  elif S[i] == 'G':
    gcount += 1
  elif S[i] == 'B':
    bcount += 1

total = rcount * gcount * bcount
count = 0
for i in range(N-2):
  for j in range(i+1, N-1):
    k = 2*j - i
    if k < N and S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
      count += 1
print(total - count)

Submission status: Accepted

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

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

for i in range(n):
    for j in range(i + 1, n):
        k = j - i + j
        if k < n:
            if {s[i], s[j], s[k]} == {"R", "G", "B"}:
                ans -= 1

print(ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

N = int(input())
S = list(str(input()))

# R,G,B ひとつずつ
# 距離が等間隔でない

R_pos = []
G_pos = []
B_pos = []
for i in range(N):
    if S[i] == "R":
        R_pos.append(i)
    elif S[i] == "G":
        G_pos.append(i)
    elif S[i] == "B":
        B_pos.append(i)

answer = len(R_pos) * len(G_pos) * len(B_pos)
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[j] != S[k] and S[k] != S[i]:
            answer -= 1

print(answer)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
R = s.count('R')
G = s.count('G')
B = s.count('B')

count = R*G*B
for d in range((N-1)//2):
    for i in range(N - (d+1)*2):
        if s[i] != s[i+d+1] and s[i] != s[i+2*(d+1)] and s[i+d+1] != s[i+2*(d+1)]:
            count -= 1
print(count)

Submission status: Accepted

Solution code:
from sys import stdin


def get_result(data):
    # N = int(data[0][0])
    S = data[1][0]
    r_index = [i for i in range(len(S)) if S[i] == 'R']
    g_index = [i for i in range(len(S)) if S[i] == 'G']
    b_index = set([i for i in range(len(S)) if S[i] == 'B'])
    all_case = len(r_index) * len(g_index) * len(b_index)
    invalid_case = 0
    for i in r_index:
        for j in g_index:
            s_i, s_j = sorted([i, j])
            tmp_max_b = s_j + (s_j-s_i)
            if tmp_max_b in b_index:
                invalid_case += 1
            tmp_min_b = s_i - (s_j-s_i)
            if tmp_min_b in b_index:
                invalid_case += 1
            tmp_mid_b = (s_i + s_j) / 2
            if tmp_mid_b in b_index:
                invalid_case += 1
    return all_case - invalid_case


if __name__ == '__main__':
    raw_data = [val.rstrip() for val in stdin.readlines()]
    data = [list(map(str, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)

Submission status: Accepted

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

r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')

ans = r_cnt * g_cnt * b_cnt

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = list(input())
    r = 0
    g = 0
    b = 0
    # print(s)
    for i, rgb in enumerate(s):
        if rgb == 'R':
            r += 1
        elif rgb == 'G':
            g += 1
        else:
            b += 1


    # print(r)
    ans = 0
    mid = 0
    # print('A')
    for i, rgb in enumerate(s):
        # print(i)

        # print('A')
        for j in range(1,i+1):
            # print(i,j,rgb)
            if i+j >= n or i-j < 0:
                break
            # print(s[i-j],rgb,s[i+j])
            if rgb != s[i-j] and s[i-j] != s[i+j] and rgb != s[i+j]:
                mid += 1


    print(r * g * b - mid)
    # # print(mid)
    # for i, ss in enumerate(s):
    #     if i == 0:
    #         continue
    #     if ss == 'R':
    #         tmp = g[i-1] * (b[-1] - b[i])
    #         tmp += b[i-1] * (g[-1] - g[i])
    #         tmp -= mid[i]
    #     elif ss == 'G':
    #         tmp = r[i-1] * (b[-1] - b[i])
    #         tmp += b[i-1] * (r[-1] - r[i])
    #         tmp -= mid[i]
    #     else:
    #         tmp = g[i-1] * (r[-1] - r[i])
    #         tmp += r[i-1] * (g[-1] - g[i])
    #         tmp -= mid[i]
    #     ans += tmp
    # print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
r = 0
g = 0
b = 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):
        if s[i] == s[j]:
            continue
        k = j * 2 - i
        if k >= n or s[k] == s[i] or s[k] == s[j]:
            continue
        ans -= 1

print(ans)

Submission status: Accepted

Solution code:
from math import floor,ceil,sqrt,factorial,log
from collections import Counter, deque
def S(): return input()
def I(): return int(input())
def MS(): return map(str,input().split())
def MI(): return map(int,input().split())
def LS(): return list(MS())
def LI(): return list(MI())
def LLS(): return [list(map(str, l.split() )) for l in input()]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def LLSN(n: int): return [LS() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]

N = I()
S = S()


def check(i, j, k):
    return j-i != k-j

chars = list(S)
cnts = Counter(chars)

cnt = 0

for i in range(N):
    for j in range(i+1, N):
        k = j + j - i
        if k > N-1:
            continue
        if chars[i] != chars[j] and chars[i] != chars[k] and chars[j] != chars[k]:
                cnt += 1

print(cnts["R"] * cnts["G"] * cnts["B"] - cnt)

Submission status: Accepted

Solution code:
n,s=open(0);n=int(n);c=s.count;print(c('R')*c('G')*c('B')-sum(len(set(s[i:j+1:j-i>>1]))>2for i in range(n-2)for j in range(i+2,n,2)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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

ans = S.count('R') * S.count('G') * S.count('B')

for i in range(len(S)):
    for d in range(len(S)):
        j = i + d
        k = j + d
        if k >= N:
            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 = set()
g = set()
b = set()
for i in range(n):
    if s[i] == "R":
        r.add(i)
    if s[i] == "G":
        g.add(i)
    if s[i] == "B":
        b.add(i)
a = len(r) * len(b) * len(g)
for i in r:
    for j in b:
        k = abs(i - j)
        if i + k in g:
            a -= 1
        if i - k in g:
            a -= 1
        if j + k in g:
            a -= 1
        if j - k in g:
            a -= 1
        if (i + j) / 2 in g:
            a -= 1
print(a)

Submission status: Accepted

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

R = S.count('R')
G = S.count('G')
B = N - R - G

ans = R * G * B

for i in range(0, N-2):
  for j in range(i+1,N-1):
    k = 2 * j - i
    if k >= N:
      break
    else:
      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()
all=s.count('R')*s.count('G')*s.count('B')
a=0
for i in range(N):
  for j in range(1,(N-i)//2+1 ):
    if i + 2*j>=N:
      break
    if s[i]!=s[i+j] and s[i]!=s[i+2*j] and s[i+j] !=s[i+2*j]:
      a += 1
print(all-a)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = list(input())
    r = [0] * n
    g = [0] * n
    b = [0] * n
    # print(s)
    for i, rgb in enumerate(s):
        if rgb == 'R':
            r[i] += 1
        elif rgb == 'G':
            g[i] += 1
        else:
            b[i] += 1

        if i != 0:
            r[i] += r[i-1]
            g[i] += g[i-1]
            b[i] += b[i-1]
    # print(r)
    ans = 0
    mid = [0] * n
    # print('A')
    for i, rgb in enumerate(s):
        # print(i)

        # print('A')
        for j in range(1,i+1):
            # print(i,j,rgb)
            if i+j >= n or i-j < 0:
                break
            # print(s[i-j],rgb,s[i+j])
            if rgb != s[i-j] and s[i-j] != s[i+j] and rgb != s[i+j]:
                mid[i] += 1


    print(r[-1] * g[-1] * b[-1] - sum(mid))
    # # print(mid)
    # for i, ss in enumerate(s):
    #     if i == 0:
    #         continue
    #     if ss == 'R':
    #         tmp = g[i-1] * (b[-1] - b[i])
    #         tmp += b[i-1] * (g[-1] - g[i])
    #         tmp -= mid[i]
    #     elif ss == 'G':
    #         tmp = r[i-1] * (b[-1] - b[i])
    #         tmp += b[i-1] * (r[-1] - r[i])
    #         tmp -= mid[i]
    #     else:
    #         tmp = g[i-1] * (r[-1] - r[i])
    #         tmp += r[i-1] * (g[-1] - g[i])
    #         tmp -= mid[i]
    #     ans += tmp
    # print(ans)

if __name__=='__main__':
    main()

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
sub = 0
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 or S[k] == S[i] or S[k] == S[j]:
            continue
        sub += 1
print(ans - sub)

Submission status: Accepted

Solution code:
import collections
N = int(input())
S = input()
minus = 0
for i in range(0, N):
    for j in range(1, N):
        if i+j*2 >= N:
            break
        if S[i] != S[i+j]:
            if S[i] != S[i+j*2]:
                if S[i+j] != S[i+j*2]:
                    minus += 1

count_dict = collections.Counter(S)

# if "R" in count_dict and "G" in count_dict and "B" in count_dict:
print(count_dict["R"]*count_dict["G"]*count_dict["B"]-minus)

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]=="B":
        b+=1
    else:
        g+=1
ans=r*b*g

for i in range(n):
    for g in range(i+1,n):
        if s[i]!=s[g] and 2*g-i<n:
            if s[2*g-i]!=s[i] and s[2*g-i]!=s[g]:
                ans-=1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r = []
g = []
b = []

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

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

print(ans)

Submission status: Accepted

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

res = 0

Rn = S.count('R')
Gn = S.count('G')
Bn = S.count('B')

res = Rn * Gn * Bn

for p in range(1, N - 1):
  for q in range(1, min(p, N - p - 1) + 1):
    i, j, k = p - q, p, p + q
    if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
      res -= 1

print(res)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
#N, M = map(int, input().split())
#A = list(map(lambda x: int(x)*(-1), input().split()))
#A = [int(input()) for i in range(N)]
#C = [list(map(int, input().split())) for i in range()]
R  =[]
G  =[]
B  =[]
for i, s in enumerate(S):
    if s == 'R':
        R.append(i)
    elif s == 'G':
        G.append(i)
    else:
        B.append(i)
answer = len(R)*len(G)*len(B)
hiku = 0

for i in range(1, (N-1)//2+1):
    #for j in range(N-i-i-2):
    j = 0
    while j + i*2 < N:
        if S[j] != S[j+i] and S[j+i] != S[j+i+i] and S[j] != S[j+i+i]:
            hiku += 1
        j+= 1
answer -= hiku
print(answer)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
a = s.count("R")
b = s.count("G")
c = s.count("B")
z = a*b*c
R=[]
G=[]
B=[]
for i in range(n):
  if s[i]=="R":
    R.append(i)
  elif s[i]=="G":
    G.append(i)
  else:
    B.append(i)
x = 0
for i in R:
  for j in G:
    if 0 <= 2*j-i <= n-1 and s[2*j-i]=="B":
      x+=1
    if 0 <= 2*i-j <= n-1 and s[2*i-j]=="B":
      x+=1
    if (i+j)%2==0 and s[(i+j)//2]=="B":
      x+=1
print(z-x)

Submission status: Accepted

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

c = Counter(S)
ans = c['B']*c['G']*c['R']
for i in range(N-1):
    for j in range(i, N):
        if S[i]==S[j]:
            continue
        elif i+(j-i)*2>N-1:
            break
        s = S[i+(j-i)*2]
        if s!=S[i] and s!=S[j]:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
s = input()
rl = []
gl = []
bl = []
for i in range(n):
    if s[i] == "R": rl.append(i+1)
    elif s[i] == "G": gl.append(i+1)
    else: bl.append(i+1)

ans = len(rl)*len(gl)*len(bl)

for i in rl:
    for j in gl:
        lb = len(bl)
        if (i+j)%2==0:
            index = bisect.bisect_left(bl, (i+j)//2)
            if lb>index and bl[index] == ((i+j)//2):
                ans -= 1
        if 2*i-j>0:
            index = bisect.bisect_left(bl, 2*i-j)
            if lb>index and bl[index] == (2*i-j):
                ans -= 1
        if 2*j-i>0:
            index = bisect.bisect_left(bl, 2*j-i)
            if lb>index and bl[index] == (2*j-i):
                ans -= 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
noCount = {'R': 0, 'G': 0, 'B': 0}
minusCount = 0
for i in S:
    if i == 'R':
        noCount['R'] += 1
    elif i == 'G':
        noCount['G'] += 1
    elif i == 'B':
        noCount['B'] += 1
RGB = noCount['R']*noCount['G']*noCount['B']

for step in range(1, (N+1)//2):
    for cenPos in range(step, N-step):
        if S[cenPos - step] != S[cenPos] and S[cenPos] != S[cenPos + step] and S[cenPos - step] != S[cenPos + step]:
            minusCount += 1
print(RGB - minusCount)

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
count = 0
sum_RGB = s.count("R") * s.count("G") * s.count("B")
for i in range(n):
    for j in range(i, n):  # rangeをiからにすることでループ減らせる
        # for k in range(n): # sum_RGBから減らしていくのでｋがi,jで決まる
        k = 2*j - i
        if k < n:
            a = (s[i], s[j], s[k])
            # print(a)
            a_set = set(a)
            # print(a_set)
            if len(a_set) == 3:
                count += 1

print(sum_RGB - count)

Submission status: Accepted

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

rc = s.count('R')
gc = s.count('G')
bc = s.count('B')
ans = rc * gc * bc

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

Submission status: Accepted

Solution code:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
# import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""
from collections import Counter
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))
# WideIntPoint


def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  # SingleInt


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  # HeightString


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  # MatrixInt


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  # NumberGrid


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def main():
    N = si()
    S = ss()
    rr = 0
    gg = 0
    bb = 0
    ans = 0
    for i in range(N):
        if S[i] == 'R':
            rr += 1
        elif S[i] == 'B':
            bb += 1
        elif S[i] == 'G':
            gg += 1
    ans = rr*gg*bb
    for i in range(N-2):
        for j in range(i+1, N-1):
            if S[i] != S[j] and 2*j-i < N and S[i] != S[2*j-i] and S[j] != S[2*j-i]:
                ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r_cnt = s.count("R")
g_cnt = s.count("G")
b_cnt = s.count("B")

sub = r_cnt * g_cnt * b_cnt

for i in range(n):
    for d in range(n):
        j = i+d
        k = j+d
        if k >= n:
            break

        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            sub-=1
print(sub)

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    S = input()
    rc = S.count('R')
    bc = S.count('B')
    gc = S.count('G')

    cnt = rc * bc * gc

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

if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

r = []
g = []
b = []

for k in range(n):
    if s[k] =="R":
        r.append(k)
        for p in range(0,min(k+1,n-k)):
            if {s[k-p], s[k+p]} == {"G", "B"}:
                sums -=1
    elif s[k] =="G":
        g.append(k)
        for p in range(0,min(k+1,n-k)):
            if {s[k-p], s[k+p]} == {"R", "B"}:
                sums -=1
    else:
        b.append(k)
        for p in range(0,min(k+1,n-k)):
            if {s[k-p], s[k+p]} == {"R", "G"}:
                sums -=1

sums +=len(r)*len(g)*len(b)
print(sums)

Submission status: Accepted

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

bad_list = ['RGB', 'RBG', 'GRB', 'GBR', 'BRG', 'BGR']
res = S.count("R") * S.count("G") * S.count("B")
for (i, s) in enumerate(S):
  
  for j in range(((i+1)//2)+1)[1:]:
    if S[i::-j][:3] in bad_list:
      res -= 1


print(res)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r = []
g = []
b = []

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = int(readline())
s = input()
rgb = [0, 0, 0]
for i in range(n):
    if s[i] == 'R':
        rgb[0] += 1
    elif s[i] == 'G':
        rgb[1] += 1
    else:
        rgb[2] += 1
ans = rgb[0] * rgb[1] * rgb[2]
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[j] != s[k] and s[i] != s[k]:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10, gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
#from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = True

n = INT()
s = STR()
t = Counter(s)
ans = t['R'] * t['G'] * t['B']
for i in range(n):
    for j in range(i + 1, n):
        k = j + (j - i)
        if k < n and set([s[i], s[j], s[k]]) == set(['R', 'G', 'B']):
            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")
point = 0
#全ての組み合わせ
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] and s[j] != s[k] and s[k] != s[i]:
            point += 1
print(R*G*B-point)

Submission status: Accepted

Solution code:
n=int(input());s=input();c=s.count;print(c('R')*c('G')*c('B')-sum(len(set(s[i:j+1:j-i>>1]))>2for i in range(n-2)for j in range(i+2,n,2)))

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
S_l = len(S)
ans = 0
cum = [[0]*3 for _ in range(N+1)]
for i,s in enumerate(S,1):
    cum[i]=cum[i-1].copy()
    if s=='R':
        cum[i][0]+=1
    elif s=='G':
        cum[i][1]+=1
    elif s=='B':
        cum[i][2]+=1


for i in range(len(S)):
  for j in range(i+1,len(S)):
    if S[i]==S[j]:
      continue
    else:
      x = 'RGB'.replace(S[i],'').replace(S[j],'')
      if x=='R':
        ans+=cum[-1][0]-cum[j][0]
        y = 2*j-i
        if y<S_l:
          if S[y]=='R':
            ans-=1
      elif x=='G':
        ans+=cum[-1][1]-cum[j][1]
        y = 2*j-i
        if y<S_l:
          if S[y]=='G':
            ans-=1
      else:
        ans+=cum[-1][2]-cum[j][2]
        y = 2*j-i
        if y<S_l:
          if S[y]=='B':
            ans-=1
 
print(ans)