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:
# coding: utf-8

# 標準入力 <int>
N = int(input())

# 標準入力<str>
S = input()

R = S.count("R")
G = S.count("G")
B = S.count("B")

cnt = 0

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

print(R * G * B - cnt)

Submission status: Accepted

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

N = int(input())
S = input()[:-1]
accR = [0]
accG = [0]
accB = [0]

for i in range(N):
    accR.append(accR[-1]+(1 if S[i]=='R' else 0))
    accG.append(accG[-1]+(1 if S[i]=='G' else 0))
    accB.append(accB[-1]+(1 if S[i]=='B' else 0))

ans = 0

for i in range(N):
    for j in range(i+1, N):
        if S[i]==S[j]:
            continue
        
        s = {S[i], S[j]}
        k = 2*j-i
        
        if 'R' not in s:
            ans += accR[N]-accR[j+1]
            
            if k<N and S[k]=='R':
                ans -= 1
        elif 'G' not in s:
            ans += accG[N]-accG[j+1]
            
            if k<N and S[k]=='G':
                ans -= 1
        else:
            ans += accB[N]-accB[j+1]
            
            if k<N and S[k]=='B':
                ans -= 1
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
cnt = 0

cnt = s.count("R") * s.count("B") * s.count("G")
# j - i = k -j を求める。
sub = 0 
for i in range(n):
    for j in range(i, n):
        k = 2 * j - i
        if k <= n - 1:
            if (s[i] != s[j]) and (s[j] != s[k]) and (s[i] != s[k]):
                sub += 1

print(cnt-sub)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
s = str(input())
r = 0
g = 0
for i in range(n):
  if s[i] == "R":
    r += 1
  elif s[i] == "G":
    g += 1
c = r*g*(n-r-g)
for j in range(1,n-1):
  for i in range(1,min(n-j-1,j)+1):
    if (s[j-i] != s[j]) and (s[j] != s[j+i]) and (s[j+i] != s[j-i]):
      c -= 1
print(c)

Submission status: Accepted

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

# print(s)

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

# print('total RGB: ', r * g * b)

ans = r * g * b

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

print(ans)

Submission status: Accepted

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

# R, G, B が文字列中にそれぞれいくつあるか数える
rcnt = s.count('R')
gcnt = s.count('G')
bcnt = s.count('B')

# 1 つ目の条件だけの通り数
ans = rcnt * gcnt * bcnt

# 2 つ目の条件を引く
# s[i], s[j], s[k] が全て違い、j - i = k - j となる場合を ans から引く事を考える
sub = 0
for i in range(n):
    for j in range(i+1, n):

        # j - i = k - j となる
        k = j + (j - i)
        if n <= k: break

        # s[i], s[j], s[k] が全て違うとき、len(alp) == 3 になる
        alp = set()
        alp.add(s[i])
        alp.add(s[j])
        alp.add(s[k])

        if len(alp) == 3: sub += 1

ans -= sub

print(ans)

Submission status: Accepted

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

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

    r_list = set()
    g_list = set()
    b_list = set()

    for i in range(n):
        if s[i] == 'R':
            r_list.add(i)
        elif s[i] == 'G':
            g_list.add(i)
        elif s[i] == 'B':
            b_list.add(i)

    for r in r_list:
        for g in g_list:
            L = g - r
            if g + L in b_list:
                sum_count -= 1
            if r - L in b_list:
                sum_count -= 1
            if L % 2 == 0 and L//2 + r in b_list:
                sum_count -= 1

    print(sum_count)

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')
cnt = 0

for i in range(n):
  for j in range(i+1,n):
	# j-i == k-j <=> k == 2j-i
    k = 2*j-i
    if k >= n or k <= j:
      break
    if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
      cnt += 1
print(r*g*b - cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
a_li = []
b_li = []
c_li = []
for i,ss in enumerate(s):
    if ss == 'R':
        a_li.append(i+1)
    elif ss == 'G':
        b_li.append(i+1)
    else:
        c_li.append(i+1)
count = 0
for i in range(1,n+1):
    for d in range(1,n):
        if i + 2*d > n:
            break
        if s[i-1] != s[i+d-1] and s[i+d-1] != s[i+2*d-1] and s[i-1] != s[i+2*d-1]:
            count += 1
#print(len(a_li),len(b_li),len(c_li),count)
#print(count)
print(len(a_li)*len(b_li)*len(c_li)-count)

Submission status: Accepted

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

R = s.count('R')
G = s.count('G')
B = n - R - G

err = 0
for i in range(n - 2):
    span = 1
    while i + 2 * span < n:
        if s[i] != s[i + span] and s[i + span] != s[i + 2 * span] and s[i + 2 * span] != s[i]:
            err += 1
        span += 1

print(R * G * B - err)

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-2):
    for j in range(i+1,n-1):
        k=2*j-i
        if k<n:
            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
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

N = i_i()
S = i_s()

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

cnt = 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[j] != S[k] and S[i] != S[k]:
            cnt += 1

print((r * g * b) - cnt)

Submission status: Accepted

Solution code:
def binary_search(list,target):

	result = -1
	left = 0
	right = len(list) - 1

	while left <= right:
		center = int((left + right)/2)
		if list[center] == target:
			result = center
			break
		elif list[center] < target:
			left = center + 1
		elif list[center] > target:
			right = center - 1

	if result == -1:
		return result
	else:
		return result


if __name__=="__main__":
    n=int(input())
    s=list(input())
    rlist=[]
    glist=[]
    blist=[]
    ans=0
    if len(s)<=3:
        print(0)
        exit()
    for i in range(n):
        if s[i]=="R":
            rlist.append(i)
        elif s[i]=="G":
            glist.append(i)
        else:
            blist.append(i)
    if len(rlist)==0 or len(glist)==0 or len(blist)==0:
        print(0)
        exit()
    ans+=len(rlist)*len(glist)*len(blist)
    for i in range(n-2):
        for j in range(i+1,n-1):
            k=2*j-i
            if s[i]==s[j]:
                continue
            elif k>len(s)-1:
                continue
            else:
                if s[k]!=s[i] and s[k]!=s[j]:
                    ans-=1

    print(ans)

Submission status: Accepted

Solution code:
input()
s = input()

r, g, b = map(s.count, 'RGB')

print(r * g * b - sum(1 for i, c1 in enumerate(s) for j, c2 in enumerate(s[i::2]) if c1 != c2 != s[i + j] != c1))

Submission status: Accepted

Solution code:
import itertools
import math
import string
import collections
from collections import Counter
from collections import deque
import sys
sys.setrecursionlimit(2*10**5)
INF = 2**60


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


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]


N = int(input())
S = input()
L = [None]*3
L[0] = []
L[1] = []
L[2] = []
for i in range(N):
    if S[i] == 'R':
        L[0].append(i)
    if S[i] == 'G':
        L[1].append(i)
    if S[i] == 'B':
        L[2].append(i)
cnt = 0
wa = [None]*3
for i in range(3):
    wa[i] = [0]*(N+1)
for i in range(3):
    for j in range(N):
        if S[j] == "RGB"[i]:
            wa[i][j+1] = 1
for i in range(3):
    for j in range(N):
        wa[i][j+1] = wa[i][j]+wa[i][j+1]
for x in itertools.permutations([0, 1, 2]):
    r = L[x[0]]
    g = L[x[1]]
    b = L[x[2]]
    for i in range(len(r)):
        for j in range(len(g)):
            if r[i] < g[j]:
                cnt += wa[x[2]][N]-wa[x[2]][g[j]+1]
                if len(S) > (g[j]-r[i])+g[j] and S[(g[j]-r[i])+g[j]] == "RGB"[x[2]]:
                    cnt -= 1
print(cnt)

Submission status: Accepted

Solution code:
N =int(input())
S = str(input())
A = 0
R = []
G = []
B = []
for i in range(len(S)):
  if S[i] == "R":
    R.append(i+1)
  elif S[i] == "G":
    G.append(i+1)
  else:
    B.append(i+1)

A = len(R)*len(G)*len(B)
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[i] != S[k]:
      A -= 1
print(A)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r = 0
if n > 2:
    r = s.count('R') * s.count('G') * s.count('B')
    for i in range(n - 2):
        k = i + 2
        for j in range(i + 1, n - 1):
            k += 2 if j > i + 1 else 0
            if k >= n:
                break
            r -= 1 if s[i] != s[j] and s[i] != s[k] and s[j] != s[k] else 0
print(r)

Submission status: Accepted

Solution code:
# RGBの数列からから'R,G,B'を取り出す必要がある．
# RGBの組み合わせのなかから(r*g*b)の中から，例えば，Rを固定して，Rからの距離の絶対値が一致するようなものは
# iを固定して， j-i=k-j= l として j,kの位置で，i以外の２文字になっているかを確認する．
# i
from collections import deque
N=int(input())
S=input()
R=deque()
G=deque()
B=deque()
for i,s in enumerate(S):
    if s=='R':
        R.append(i)
    if s == 'G':
        G.append(i)
    if s == 'B':
        B.append(i)

rgb = len(R)*len(G)*len(B)
import bisect
# ワンホット表現にしてみる
R_one = [0 for _ in range(N+3)]
G_one = [0 for _ in range(N+3)]
B_one = [0 for _ in range(N+3)]

for r in R:
    R_one[r] = 1
for g in G:
    G_one[g] = 1
for b in B:
    B_one[b] = 1

for i in range(0,N):
    if i in R:
        lest1 = G_one
        lest2 = B_one
    elif i in G:
        lest1 = B_one
        lest2 = R_one
    else:
        lest1 = R_one
        lest2 = G_one
    max_j_width = int((N-1-i)/2)+1
    for j in range(1,max_j_width+1):
        if lest1[i+j]== lest2[i+2*j] == 1:
            rgb-=1
        elif lest2[i + j] == lest1[i + 2 * j] == 1:
            rgb-=1
print(rgb)

Submission status: Accepted

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

for s in S:
    if s=="R":
        r+=1
    elif s=="G":
        g+=1
    else:
        b+=1

ans = r*g*b

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

Submission status: Accepted

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

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

ruisekiR = [0] * (n+1)
ruisekiG = [0] * (n+1)
ruisekiB = [0] * (n+1)

for i,c in enumerate(s):
    ruisekiR[i+1] = ruisekiR[i] + ( 1 if c == "R" else 0)
    ruisekiG[i+1] = ruisekiG[i] + ( 1 if c == "G" else 0)
    ruisekiB[i+1] = ruisekiB[i] + ( 1 if c == "B" else 0)

# log(ruisekiR)
# log(ruisekiG)
# log(ruisekiB)

ans = 0

for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] == s[j]:
            continue
        c = "R"
        if "R" in (s[i], s[j]):
            c = "G"
            if "G" in (s[i], s[j]):
                c = "B"

        # log(s[i], s[j], c)

        if c == "R":
            ans += (ruisekiR[n] - ruisekiR[j])
        if c == "G":
            ans += (ruisekiG[n] - ruisekiG[j])
        if c == "B":
            ans += (ruisekiB[n] - ruisekiB[j])

        if j+(j-i)<n and s[j+(j-i)] == c:
            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')

count = 0
#count second conditions satisfied
for i in range(n):
    for j in range(i+1,n):
        #search j - i = k - j ⇔ k = 2 * j - i
        k = 2 * j - i
        if k >= n: break
        if s[i] != s[j] and s[j] != s[k] and s[i] != s[k]: count += 1
print((r * g * b) - 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):
        if (j + j - i < N) and ({S[i], S[j], S[j + j - i]} == {'R', 'G', 'B'}):
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input().rstrip()
t=S.count('R')*S.count('G')*S.count('B')
d=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[i]==S[k] or S[j]==S[k]: continue
    d+=1
print(t-d)

Submission status: Accepted

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

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

count = 0
for i in range(N):
    for j in range(i + 1, N):
        #2つ目の条件であるj - i ≠ k - jを満たさない数を求める
        #j - i = k - j の場合を調べる必要があるので、k = j + j - iとなる
        k = j + j - i
        if k >= N:
            break
        #2つ目の条件に合致しないことがi, j, k より明らかなので、1目の条件に合致しなければダメ
        if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
            count += 1
#1つ目の条件に合致する総数はr * g * bなので、2つ目の条件に合致しない数を引けば答えとなる    
print((r * g * b) - count)

Submission status: Accepted

Solution code:
N = int(input())
arr = list(input())
a = arr.count("R")
b = arr.count("G")
c = arr.count("B")
count = a * b * c

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

Submission status: Accepted

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

#BGR, BRG, GBR, GRB, RBG, RGB
r, g, b = 0, 0, 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 #条件1を満たす組みの総和

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]:
                ans -= 1
        else:
          continue
          
print(ans)

Submission status: Accepted

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

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

cnt=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[j]!=S[k] and S[k]!=S[i]:
            cnt+=1

print((r*g*b)-cnt)

Submission status: Accepted

Solution code:
# Problem D - RGB Triplets

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

# initialization
r_count = 0
g_count = 0
b_count = 0
exception_count = 0

# a, b, c count
for i in range(N):
    if S[i]=='R':
        r_count += 1
    elif S[i]=='G':
        g_count += 1
    elif S[i]=='B':
        b_count += 1
ans_1 = r_count * g_count * b_count

# exception count
for i in range(N-2):
    for j in range(i+1,N-1):
        k = 2 * j - i
        if k>=N:
            continue
        if not S[i]==S[j] and not S[j]==S[k] and not S[i]==S[k]:
            exception_count += 1

# output
ans = ans_1 - exception_count
print(ans)

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')

# print(r_cnt, g_cnt, b_cnt)
ans = r_cnt * g_cnt * b_cnt
# print(ans)

for i in range(n):
    for d in range(n):
        j = i + d
        k = j + d
        # print(i,j,k)
        if k >= n: 
            # print('/')
            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')
b = S.count('B')
g = S.count('G')
x = r*b*g

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

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

Submission status: Accepted

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

R_count = S.count('R')
G_count = S.count('G')
B_count = S.count('B')

ans = R_count * G_count * B_count

for i in range(N-2):
    for d in range(1,N):
        if i+2*d >= N:
            break
        else:
            j = i+d
            k = j+d
            
            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_count = S.count('R')
G_count = S.count('G')
B_count = S.count('B')

ans = R_count * G_count * B_count

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

print(ans)

Submission status: Accepted

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

# process
ans = S.count('R')*S.count('G')*S.count('B')
for i in range(N):
    for j in range(N):
        if i-j<0 or i+j >= N:
            break
        x = S[i-j]
        y = S[i]
        z = S[i+j]

        if x!= y and x != z and y != z:
            ans -= 1
            
# output
print(ans)

Submission status: Accepted

Solution code:
#def main():
#    N = int(input())
#    S = input()
#    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)
#    s = 0
#    for i in R:
#        for j in G:
#            for k in B:
#                if abs(i-j) != abs(j-k) and abs(i-j) != abs(k-i) and abs(j-k) != abs(k-i):
#                    s += 1
#    print(s)
#if __name__ == '__main__':
#    main()
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:
            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:
n = int(input())
s = input()

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

cnt = 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]:
			cnt -= 1


print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = input()
ans = S.count('R') * S.count('G') * S.count('B')

for i in range(N):
    for j in range(1,N):
        if (i-j<0 or i+j>=N):
            break
        l = S[i-j]
        m = S[i]
        r = S[i+j]
        if l!=m and m!=r and r!=l:
            ans -= 1
print (ans)

Submission status: Accepted

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



n=S.count("R")*S.count("G")*S.count("B")
count=0
for l in range(N-2):
    for r in range(l+2,N):
        m=(l+r)/2
        if m%1.0!=0:
            continue
        m=int(m)
        tmp =[S[l], S[r], S[m]]
        if len(set(tmp))==3:
            count+=1
            
            

print(n-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 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())
sentense = input()
count_1 = sentense.count("R")*sentense.count("G")*sentense.count("B")
count_1_and_not_2 = 0
for i in range(N):
    for j in range(i+1, N):
        k = (j-i)+j
        if k >= N:
            break
        if sentense[i] != sentense[j] and sentense[j] != sentense[k] and sentense[k] != sentense[i]:
            count_1_and_not_2 += 1

print(count_1-count_1_and_not_2)

Submission status: Accepted

Solution code:
def main():
    import sys
    from collections import Counter
    sys.setrecursionlimit(10 ** 6)
    input = sys.stdin.readline
    N = int(input())
    S = input().strip()
    cnt = Counter(S)
    ans = cnt['R'] * cnt['G'] * cnt['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] and S[i] != S[k] and S[j] != S[k]:
                ans -= 1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
length = int(input())
text = input()

r_count = text.count("R")
g_count = text.count("G")
b_count = text.count("B")

answer = r_count * g_count * b_count

for i in range(length-2):
    for j in range(i+1, length-1):
        k = i + (j-i)*2
        if k < length and text[i]!=text[j] and text[i]!=text[k] and text[j]!=text[k]:
            answer -= 1
        
print(answer)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
count = 0
r=0
g=0
b=0
#i,j,k-->i,j,2j-i
for i in range(N):
    if S[i]=="R":
        r+=1
    elif S[i]=="G":
        g+=1
    else:
        b+=1
    for j in range(i+1,N):
        if 2*j-i <= N-1:
            if S[i] != S[j] and S[j]!=S[2*j-i] and S[2*j-i]!=S[i]:
                count -= 1
count += r*g*b
print(count)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
Rnum = 0
Gnum = 0
Bnum = 0
for i in S:
    if i == 'R':
        Rnum += 1
    elif i == 'G':
        Gnum += 1
    else:
        Bnum += 1

ans = Rnum*Gnum*Bnum

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

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    N=int(input())
    S=str(input())
    R=[]
    G=[]
    B=[]
    for i in range(N):
        if S[i]=='R':
            R.append(i)
        if S[i]=='G':
            G.append(i)
        if S[i]=='B':
            B.append(i)
    lr=len(R)
    lg=len(G)
    ans=lr*lg*(N-lr-lg)

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




resolve()

Submission status: Accepted

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

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



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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=s.count("R")*s.count("G")*s.count("B")
for j in range(n):
  J=s[j]
  for i in range(j):
    k=2*j-i
    if k<n:
      I=s[i]
      K=s[k]
      if I!=J!=K!=I:
        ans-=1   
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
S=input()
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)
r=len(R)
g=len(G)
b=len(B)

c=0
for i in range(n):
	for j in range(i+1,n):
		k=2*j-i
		if k<n:
			if len(set([S[i],S[j],S[k]]))==3:
				c+=1

print(r*g*b-c)

Submission status: Accepted

Solution code:
N = int(input())
str = input()
S = []
for i in range(N):
    if str[i] == 'R':
        S.append(0)
    elif str[i] == 'G':
        S.append(1)
    else:
        S.append(2)

ans = 0
for i in range(N):
    left = [0, 0, 0]
    cent = [0, 0, 0]
    right = [0, 0, 0]
    for j in range(N):
        if j < i:
            left[S[j]] += 1
        elif i == j:
            cent[S[j]] += 1
        else:
            right[S[j]] += 1
    for a in range(3):
        for b in range(3):
            for c in range(3):
                if a == b or a == c or b == c:
                    continue
                ans += left[a] * cent[b] * right[c]
    for d in range(N):
        left = i - d
        right = i + d
        if left < 0 or right >= N or S[left] == S[i] or S[left] == S[right] or S[i] == S[right]:
            continue
        ans -= 1
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# D

import sys
# 再起回数上限変更
# sys.setrecursionlimit(1000000)
from bisect import bisect_left

N = int(input())
S = input()
R = []
G = []
B = []

cnt_r, cnt_g, cnt_b = 0, 0, 0
for idx, s in enumerate(S):
    if s == 'R':
        R.append(idx+1)
        cnt_r += 1
    if s == 'G':
        G.append(idx+1)
        cnt_g += 1
    if s == 'B':
        B.append(idx+1)
        cnt_b += 1
# print(R, G, B)

if (cnt_r > 0) and (cnt_g > 0) and (cnt_b > 0):
    pass
else:
    print(0)
    sys.exit()

R0, G0, B0 = R, G, B

sum = 0

for i in range(6):
    if i == 0:
        R, G, B, temp = R0, G0, B0, 'B'
    elif i == 1:
        R, G, B, temp = R0, B0, G0, 'G'
    elif i == 2:
        R, G, B, temp = B0, R0, G0, 'G'
    elif i == 3:
        R, G, B, temp = B0, G0, R0, 'R'
    elif i == 4:
        R, G, B, temp = G0, R0, B0, 'B'
    elif i == 5:
        R, G, B, temp = G0, B0, R0, 'R'

    for s in R:
        if s > G[-1]:
            break
        idx1 = bisect_left(G, s)
        for g in G[idx1:]:
            if g > B[-1]:
                break
            idx2 = bisect_left(B, g)
            sum += len(B[idx2:])
            if 2 * g - s <= N:
                if S[2 * g - s - 1] == temp:
                    sum -= 1

print(sum)

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 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]:
      count -=1

print(count)

Submission status: Accepted

Solution code:
from itertools import permutations

N = int(input())

R = set()
G = set()
B = set()
for i, s in enumerate(input(), start=1):
    if s == 'R':
        R.add(i)
    if s == 'G':
        G.add(i)
    if s == 'B':
        B.add(i)

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

def calc(A, B, C):
    ret = 0
    for a in A:
        for b in B:
            c = 2 * a - b
            if c in C:
                ret += 1
    return ret

ans -= calc(R, G, B)
ans -= calc(G, B, R)
ans -= calc(B, R, G)

print(ans)

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solve():
    n = II()
    S = list(input())

    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)

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

    print(len(R) * len(G) * len(B) - cnt)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
R = []
G = []
B = []
for i in range(N):
    if S[i] == 'R':
        R.append(i)
    elif S[i] == 'G':
        G.append(i)
    elif S[i] == 'B':
        B.append(i)
ans = len(R)*len(G)*len(B)
for i in range(1,(N+1)//2):
    for j in range(i,N-i):
        if S[j] != S[j-i] and S[j] != S[j+i] and S[j-i] != S[j+i]:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
ans = 0
r = S.count("R")
g = S.count("G")
b = S.count("B")
ans += r*g*b
m = 0
if N >= 3:
  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]:
        m += 1
print(ans-m)

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 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]:
      count -=1

print(count)

Submission status: Accepted

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

r=0
g=0
b=0

for i in range(n):
    if s[i]=='R':
        r+=1
    if s[i]=='G':
        g+=1
    if s[i]=='B':
        b+=1
ans=r*g*b
        
for j in range(n):
    for i in range(j):
        k=2*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 = str(input())
r = 0
g = 0
b = 0
#S[i],S[j],S[k]が違うものはr*g*b通り
for i in range(N):
  if S[i] == 'R':
    r += 1
  elif S[i] == 'G':
    g += 1
  else:
    b += 1
tot = r*g*b
#S[i],S[j],S[k]が違うかつj-i=k-jを満たすものは引く
minus = 0
for i in range(N-2):
  for j in range(i+1,N-1):
    if 2*j-i < N:
      
      if S[i] != S[j] and S[j] != S[2*j-i] and S[i] != S[2*j-i]:
        minus += 1
print(tot - minus)

Submission status: Accepted

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

r = 0
g = 0
b = 0

for c in s:
    if c == "R":
        r += 1
    elif c == "G":
        g += 1
    else:
        b += 1

all_patterns = r * g * b
sub = 0

for i in range(n):
    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
        sub += 1
print(all_patterns-sub)