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 -*-

############################################

# read data for n sequences.
N = int(input())
data = input()

out = 0
r=0;g=0;b=0;
for d in data:
    if d =="R":
        r+=1
    elif d=="B":
        b+=1
    else:
        g+=1
out = r*g*b

if N > 2:
    for ii, i in enumerate(data[:-2]):
        for jj,j in enumerate(data[ii+1:-1]):
            jj = jj+ii+1
            if i!=j:
                sum1 = 0                    
                for d in [d for d in "RGB"]:
                    if i!=d and j!=d:
                        let = d

                if jj+jj-ii<N:
                    if data[jj+jj-ii]==let:                    
                        out-=1
print(out)

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

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

print(r_count*g_count*b_count-discount)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
r = S.count('R')
g = S.count('G')
b = S.count('B')
c = 0
for i in range(N-2):
  for j in range(i+1,(i+N+1)//2):
    k = j*2-i
    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())
S=input()
totalPattern = S.count("R")*S.count("G")*S.count("B")
for i in range(N-1):
    for j in range(i+1,N):
        k=j+(j-i)
        if k>N-1:
            continue
        if not S[i] == S[j] and not S[i] == S[k] and not S[j] == S[k]:
            totalPattern-=1
print(totalPattern)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
a = []
for i in range(len(S)):
  if S[i] == 'R':
    a.append(0)
  elif S[i] == 'G':
    a.append(1)
  elif S[i] == 'B':
    a.append(2)
    
cnt = [0] * 3
for i in range(N):
  cnt[a[i]] += 1
ans = cnt[0] * cnt[1] * cnt[2]

for j in range(N):
  for i in range(j):
    k = j + (j - i)
    if k < N:
      if a[i] == a[j]:
        continue
      if a[i] == a[k]:
        continue
      if a[k] == a[j]:
        continue
      ans -= 1
      
print(ans)

Submission status: Accepted

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

    for i in range(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]:
                    ans -= 1
    print(ans)


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):
        if j*2-i < N:
            if (S[i] != S[j]) and (S[i] !=S[(j*2)-i]) and (S[j] != S[(j*2)-i]):
                cnt += 1
            else:
                pass
        else:
            pass
print(R*G*B - cnt)

Submission status: Accepted

Solution code:
N=int(input())
S=list(input())
a=[0]*4

for i in range(N):
  if S[i]=='R':a[0]+=1
  if S[i]=='G':a[1]+=1
  if S[i]=='B':a[2]+=1
    
for i in range(N):
  for j in range(i+1,N):
    k=j+(j-i)
    if k>=N:
      break
    else:
      if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i] and j-i==k-j:
        a[3]+=1

print(a[0]*a[1]*a[2]-a[3])

Submission status: Accepted

Solution code:
n=int(input())
s=str(input())
r,g,b=0,0,0
ans=0
for i in s:
    if i == 'R':
        r += 1
    elif i == 'G':
        g += 1
    else:
        b += 1
for i in range(0,n):
    if s[i]=='R':
        ans += b*g
        r -= 1
    elif s[i]=='G':
        ans += r*b
        g -= 1
    else:
        ans += r*g
        b -= 1
for i in range(1,n):
    for j in range(n):
        l,mid,r=j,j+i,j+i+i
        if r<n:
            d={}
            d[s[l]]=1
            d[s[mid]]=1
            d[s[r]]=1
            if len(d)==3:
                ans -= 1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
R,G,B=0,0,0
ans=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
f,s,t=0,1,2
while t<=N-1:
    i=0
    while i+t<=N-1:
        if S[f+i]!=S[s+i] and S[f+i]!=S[t+i] and S[s+i]!=S[t+i]:
            ans-=1
        i+=1
    s+=1
    t+=2
print(ans)

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 s[i]!=s[j]:
            x = 2*j-i
            if x<n and s[i]!=s[x] and s[j]!=s[x]:
                ans -= 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = str(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 j in range(i+1, n-1):
        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()

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import os
from io import BytesIO, IOBase

#########################
# imgur.com/Pkt7iIf.png #
#########################

# returns the list of prime numbers less than or equal to n:
'''def sieve(n):
    if n < 2: return list()
    prime = [True for _ in range(n + 1)]
    p = 3
    while p * p <= n:
        if prime[p]:
            for i in range(p * p, n + 1, p):
                prime[i] = False
        p += 2
    r = [2]
    for p in range(3, n + 1, 2):
        if prime[p]:
            r.append(p)
    return r'''

# returns all the divisors of a number n(takes an additional parameter start):
'''def divs(n, start=1):
    divisors = []
    for i in range(start, int(n**.5) + 1):
        if n % i == 0:
            if n / i == i:
                divisors.append(i)
            else:
                divisors.extend([i, n // i])
    return len(divisors)'''

# returns the number of factors of a given number if a primes list is given:
'''def divn(n, primes):
    divs_number = 1
    for i in primes:
        if n == 1:
            return divs_number
        t = 1
        while n % i == 0:
            t += 1
            n //= i
        divs_number *= t
    return divs_number'''

# returns the leftmost and rightmost positions of x in a given list d(if x isnot present then returns (-1,-1)):
'''def flin(d, x, default=-1):
    left = right = -1
    for i in range(len(d)):
        if d[i] == x:
            if left == -1: left = i
            right = i
    if left == -1:
        return (default, default)
    else:
        return (left, right)'''

#count xor of numbers from 1 to n:
'''def xor1_n(n):
    d={0:n,1:1,2:n+1,3:0}
    return d[n&3]'''

def cel(n, k): return n // k + (n % k != 0)
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(map(int, input().split()))
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
def prr(a, sep=' '): print(sep.join(map(str, a)))
def dd(): return defaultdict(int)
def ddl(): return defaultdict(list)
def ddd(): return defaultdict(defaultdict(int))

# region fastio
 
BUFSIZE = 8192
 
 
class FastIO(IOBase):
	newlines = 0
 
	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = "x" in file.mode or "r" not in file.mode
		self.write = self.buffer.write if self.writable else None
 
	def read(self):
			while True:
					b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
					if not b:
							break
					ptr = self.buffer.tell()
					self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
			self.newlines = 0
			return self.buffer.read()
 
	def readline(self):
			while self.newlines == 0:
					b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
					self.newlines = b.count(b"\n") + (not b)
					ptr = self.buffer.tell()
					self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
			self.newlines -= 1
			return self.buffer.readline()
 
	def flush(self):
			if self.writable:
					os.write(self._fd, self.buffer.getvalue())
					self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
	def __init__(self, file):
			self.buffer = FastIO(file)
			self.flush = self.buffer.flush
			self.writable = self.buffer.writable
			self.write = lambda s: self.buffer.write(s.encode("ascii"))
			self.read = lambda: self.buffer.read().decode("ascii")
			self.readline = lambda: self.buffer.readline().decode("ascii")
 

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

from collections import defaultdict 
#from collections import deque
#from collections import OrderedDict
#from math import gcd
#import time
#import itertools
#import timeit
#import random
#from bisect import bisect_left as bl
#from bisect import bisect_right as br
#from bisect import insort_left as il
#from bisect import insort_right as ir
#from heapq import *
#from queue import PriorityQueue
#mod=998244353
#mod=10**9+7
# for counting path pass prev as argument:
# for counting level of each node w.r.t to s pass lvl instead of prev:

n=ii()
s=input()
r,g=[],[]
B=dd()
for i in range(n):
    if s[i]=='R':
        r.append(i+1)
    elif s[i]=='G':
        g.append(i+1)
    else:
        B[i+1]=True
ans=len(r)*len(g)*len(B)
for a in r:
    for b in g:
        if (a+b)&1==0 and B[(a+b)//2]:
            ans-=1
        if B[2*b-a]:
            ans-=1
        if B[2*a-b]:
            ans-=1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=str(input())
R=[]
B=[]
G=[]
for i in range(n):
  if s[i]=='R':
    R.append(i)
  elif s[i]=='B':
    B.append(i)
  else:
    G.append(i)
ans=len(R)*len(B)*len(G)

for i in range(n-1):
  for j in range(i+1,n):
    char1=s[i]
    char2=s[j]
    if char1==char2:
      pass
    else:
      if n>2*j-i>j and s[2*j-i]!=char1 and s[2*j-i]!=char2:
        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 i in range(n):
  for diff in range(n):
    j = i + diff
    k = j + diff
    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 = [0 for i in range(n+1)]
G = [0 for i in range(n+1)]
B = [0 for i in range(n+1)]
R[n] = 0
G[n] = 0
B[n] = 0

for i in range(n-1, -1, -1):
    R[i] = R[i+1] + (s[i] == 'R')
    G[i] = G[i+1] + (s[i] == 'G')
    B[i] = B[i+1] + (s[i] == 'B')

sm = 0
for i in range(n):
    for j in range(i+1,n):
        nxt = j-i + j
        f = True
        if nxt >= n:
            f = False
        if s[i] == s[j]:
            continue
        if s[i] != 'R' and s[j] != 'R':
            sm += R[j+1]
            if f:
                sm -= (s[nxt] == 'R')
        if s[i] != 'G' and s[j] != 'G':
            sm += G[j + 1]
            if f:
                sm -= (s[nxt] == 'G')
        if s[i] != 'B' and s[j] != 'B':
            sm += B[j + 1]
            if f:
                sm -= (s[nxt] == 'B')
print(sm)

Submission status: Accepted

Solution code:
from functools import reduce
from operator import mul
N = int(input())
S = input()
appearances = {"R": 0, "G": 0, "B": 0}
for c in S:
    appearances[c] += 1
print(reduce(mul, list(appearances.values())) - sum(sum(len(set(S[start] + S[start + step] + S[start + step * 2])) == 3 for start in range(N - step * 2)) for step in range(1, N // 2 + 1)))

Submission status: Accepted

Solution code:
from numpy import prod
N = int(input())
S = input()
appearances = {"R": 0, "G": 0, "B": 0}
for c in S:
    appearances[c] += 1
print(prod(list(appearances.values())) - sum(sum(len(set(S[start] + S[start + step] + S[start + step * 2])) == 3 for start in range(N - step * 2)) for step in range(1, N // 2 + 1)))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = list(input())
    R = []
    G = []
    B = []
    Reval = {}
    Geval = {}
    Beval = {}
    for i in range(N):
        if S[i] == "R":
            R.append(i)
            Reval[2*i] = 2*i
        elif S[i] == "G":
            G.append(i)
            Geval[2*i] = 2*i
        elif S[i] == "B":
            B.append(i)
            Beval[2*i] = 2*i
    count = len(R)*len(G)*len(B)

    for a in R:
        for b in G:
            if (a + b) in Beval:
                count -= 1
    for a in G:
        for b in B:
            if (a + b) in Reval:
                count -= 1
    for a in B:
        for b in R:
            if (a + b) in Geval:
                count -= 1
    print(count)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
R = []
G = []
B = []
Reval = {}
Geval = {}
Beval = {}
for i in range(N):
    if S[i] == "R":
        R.append(i)
        Reval[2*i] = 2*i
    elif S[i] == "G":
        G.append(i)
        Geval[2*i] = 2*i
    elif S[i] == "B":
        B.append(i)
        Beval[2*i] = 2*i
count = len(R)*len(G)*len(B)

for a in R:
    for b in G:
        if (a + b) in Beval:
            count -= 1
for a in G:
    for b in B:
        if (a + b) in Reval:
            count -= 1
for a in B:
    for b in R:
        if (a + b) in Geval:
            count -= 1
print(count)

Submission status: Accepted

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

N = int(input())
I = input()

R =0
G =0
B =0
for i in range(N):
    if (I[i] == 'R'):
        R+=1
    if (I[i] == 'G'):
        G+=1
    if (I[i] == 'B'):
        B+=1


sum = R * G * B

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

print(sum)

Submission status: Accepted

Solution code:
N = int(input())
S = list(map(str,input()))

r = 0
g = 0
b = 0
for i in range(N):
    if S[i] == "R":
        r+=1
    elif S[i] == "G":
        g+=1
    else:
        b+=1
ans = 0
for i in range(N):
    for j in range(1,N//2+2):
        if i+2*j < N:
            if S[i] != S[i+j] and S[i] != S[i+2*j] and S[i+j] != S[i+2*j]:
                ans +=1

print(r*g*b-ans)

Submission status: Accepted

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

r, g, b = 0, 0, 0
for i in range(n):  #O(n)
    if S[i] == 'R':
        r += 1
    elif S[i] == 'G':
        g += 1
    elif S[i] == 'B':
        b += 1
    else:
        pass

ans = r * g * b
for i in range(1, n-1): #O(n)
    d = i
    if d > n-1 - d:
        d = n-1 - d
    else:
        pass
    for j in range(1, d+1): #O(n)
        if S[i] != S[i-j] and S[i] != S[i+j] and S[i-j] != S[i+j]:
            ans -= 1
        else:
            pass
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
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=j+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:
n = int(input())
s = input()

R = 0
G = 0
B = 0

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

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

ans = R*G*B - cnt
print(ans)

Submission status: Accepted

Solution code:
n= int(input())
s_list= list(input())
count=0
rr,bb,gg=0,0,0
for i in range(0,n):
    if s_list[i]=="R":
        rr=rr+1
    elif s_list[i]=="G":
        gg=gg+1
    else:
        bb=bb+1
sum=rr*gg*bb

for p in range(0,n-2):
    for q in range(p+1,n-1):
        if s_list[p] == s_list[q]:
            continue
        elif 2*q-p<n and s_list[2*q-p]!=s_list[p] and s_list[2*q-p]!=s_list[q]:
            count= count+1
print(sum-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,N):
        k=j+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:
number = int(input())
character = input()
rnumber = character.count('R')
gnumber = character.count('G')
bnumber = character.count('B')
count = rnumber*gnumber*bnumber

for j in range(1, number):
    for i in range(j):
        k = j + (j - i)
        if k >= number:
            continue    
        if character[i] ==character[j]:
            continue
        if character[i] == character[k]:
            continue
        if character[j] == character[k]:
            continue
        count -= 1


print(count)

Submission status: Accepted

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

def main():
    N = int(input())
    S = input()
    CNTR = 0
    CNTG = 0
    CNTB = 0
    for i in range(N):
        if S[i] == 'R':
            CNTR += 1
        if S[i] == 'G':
            CNTG += 1
        if S[i] == 'B':
            CNTB += 1
    CONB = CNTR * CNTG * CNTB
    CNT = 0
    for i in range(N):
        for j in range(i,N):
            if S[j] != S[i]:
                k = j + (j-i)
                if k > N-1:
                    continue
                if S[k] != S[i] and S[k] != S[j]:
                    CNT += 1
    print(CONB - CNT)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#coding:utf-8

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

count = 0
r, g, b = 0, 0, 0

for i in s:
    if i == 'R':
        r += 1
    elif i == 'G':
        g += 1
    elif i == 'B':
        b += 1

count = r * g * b

for i in range(n-2):
    si = s[i]
    for j in range(i+1,n-1):
        sj = s[j]
        nk = 2 * j - i
        if not nk < n:
            break
        snk = s[nk]
        if {si, sj, snk} == {'R','G','B'}:
            count -= 1

print("{}".format(count))

Submission status: Accepted

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

N = int(input())
S = input()
CNTR = 0
CNTG = 0
CNTB = 0
for i in range(N):
    if S[i] == 'R':
        CNTR += 1
    if S[i] == 'G':
        CNTG += 1
    if S[i] == 'B':
        CNTB += 1
CONB = CNTR * CNTG * CNTB
CNT = 0
for i in range(N):
    for j in range(i,N):
        if S[j] != S[i]:
            k = j + (j-i)
            if k > N-1:
                continue
            if S[k] != S[i] and S[k] != S[j]:
                CNT += 1
print(CONB - CNT)

Submission status: Accepted

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

dic = {k: 0 for k in "RGB"}

for i in range(n):
  dic[s[i]] += 1
  
res = dic['R'] * dic['G'] * dic['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]: res -= 1

print(res)

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

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

print(ans-tmp)

Submission status: Accepted

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

def main():
    n = int(input())
    s_li = list(input().rstrip())

    r_li = []
    g_li = []
    b_li = []

    for i in range(n):
        if s_li[i] == 'R':
            r_li.append(i)
        elif s_li[i] == 'G':
            g_li.append(i)
        elif s_li[i] == 'B':
            b_li.append(i)

    if (len(r_li) < 1 or len(g_li) < 1) or len(b_li) < 1:
        print(0)
    else:
        all_c = len(r_li) * len(g_li) * len(b_li)

        for i in range(n):
            for j in range(i+1,n):
                k = 2*j-i
                if k >= n:
                    continue

                if (s_li[i] != s_li[j] and s_li[j] != s_li[k]) and s_li[i] != s_li[k]:
                    all_c -= 1

        print(all_c)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

arr = []
R = len(list(filter(lambda x:x == 'R', s)))
G = len(list(filter(lambda x:x == 'G', s)))
B = len(list(filter(lambda x:x == 'B', s)))
tot = 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[i] != s[k]) and (s[j] != s[k]):
      tot -= 1

print(tot)

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=j+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:
def main():
    n = int(input())
    s = input()
    # Positions for each character.
    rs = s.count('R')
    gs = s.count('G')
    bs = s.count('B')
    ans = rs * gs * bs

    # `i, j` を決めれば， `k = j + (j-i)`。 `j-i = k-j` の組合わせを引く。
    for j in range(n):
        for i in range(j):  # i < j
            k = j + (j - i)
            if k >= n:
                continue
            if s[i] == s[j]:
                continue
            if s[i] == s[k]:
                continue
            if s[j] == s[k]:
                continue

            # `j-i = k-j` かつRGB。
            ans -= 1

    return ans


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
from collections import defaultdict
counts = defaultdict(lambda: 0)

n = int(input())
s = input()
ans = 0

for i, c in enumerate(s):
    counts[c] += 1
    j = 1
    while i - j >= 0 and i + j <= n - 1:
        if s[i - j] != s[i] and s[i] != s[i + j] and s[i - j] != s[i + j]:
            ans -= 1
        j += 1
print(ans + counts['R'] * counts['G'] * counts['B'])

Submission status: Accepted

Solution code:
def main():
  N = int(input())
  S = input()
  R = 0
  G = 0
  B = 0
  nsum = 0
  for s in S:
      if s == "R":
          R += 1
      elif s == "G":
          G += 1
      else:
          B += 1
  count = 0
  for i in range(N):
      for k in range(i+1,N):
          if not S[i] == S[k]:
              if 2*k - i <= N - 1:
                  if S[i] != S[2*k - i] and S[k] != S[2*k - i]:
                      count += 1
  print(R*G*B - 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")
ans=R*G*B
for i in range(N):
    for j in range(i,N):
        k=j+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:
n = int(input())
s = input()
rl = s.count('R')
gl = s.count('G')
bl = s.count('B')
rr = 0
gr = 0
br = 0
if s[0] == 'R':
    rl -= 1
    rr += 1
elif s[0] == 'G':
    gl -= 1
    gr += 1
else:
    bl -= 1
    br += 1

ans = 0
for i in range(1, n-1):
    for j in range(1, n//2+1):
        if i-j < 0 or i+j > n-1:
            break
        if s[i+j] != s[i-j] and s[i+j] != s[i] and s[i-j] != s[i]:
            ans -= 1
    if s[i] == 'R':
        ans += bl*gr + br*gl
        rl -= 1
        rr += 1
    elif s[i] == 'G':
        ans += rr*bl + rl*br
        gl -= 1
        gr += 1
    else:
        ans += gr*rl + gl*rr
        bl -= 1
        br += 1
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    l = list(input())
    ans = l.count('R') * l.count('G') * l.count('B')
    for c in range(n):
        center = l[c]
        for dif in range(n):
            if dif + c > n - 1 or c - dif < 0:
                continue
            left = l[c - dif]
            right = l[dif + c]
            if left != center and center != right and right != left:
                ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
(r,g,b) = (0,0,0)
for i in s:
	if i == "R":
		r += 1
	elif i == "G":
		g += 1
	else:
		b += 1
a = r*g*b
t = 0
for i in range(n-2):
	for j in range(i+1,n-1):
		if 2*j-i <= n-1 and s[i]!=s[j] and s[j]!=s[2*j-i] and s[2*j-i]!=s[i]:
			t += 1
print (a-t)

Submission status: Accepted

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

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


ans=r*g*b
dib=0
m=0

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
CNTR = 0
CNTG = 0
CNTB = 0
for i in range(N):
    if S[i] == 'R':
        CNTR += 1
    if S[i] == 'G':
        CNTG += 1
    if S[i] == 'B':
        CNTB += 1
CONB = CNTR * CNTG * CNTB
CNT = 0
for i in range(N):
    for j in range(i,N):
        if S[j] != S[i]:
            k = j + (j-i)
            if k > N-1:
                continue
            if S[k] != S[i] and S[k] != S[j]:
                CNT += 1
print(CONB - CNT)

Submission status: Accepted

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

num_r = S.count('R')
num_g = S.count('G')
num_b = S.count('B')

ans = num_r * num_g * num_b

for i in range(0, N) :
  for temp in range(1, int((N-i+1)/2)) :
    j = i + temp
    k = i + 2 * temp
    c1 = S[i]
    c2 = S[j]
    c3 = S[k]
    if((c1 != c2) and (c2 != c3) and (c3 != c1)) :
      ans -= 1
        
        
print(ans)

Submission status: Accepted

Solution code:
N = int(input())  # 1 <= N <= 4000
S = input()  # [RGB]{N}
L = {
    'R': tuple(i for i, v in enumerate(S) if v == 'R'),
    'G': tuple(i for i, v in enumerate(S) if v == 'G'),
    'B': tuple(i for i, v in enumerate(S) if v == 'B')
}
R_length = len(L['R'])
G_length = len(L['G'])
B_length = len(L['B'])

ans = R_length * G_length * B_length
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]:
            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")

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[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()

c = Counter(S)

ans = c['R'] * c['G'] * c['B']

for d in range(1, 4000):
    for i in range(N):
        j = i + d
        k = j + d
        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()
ans = s.count('R') * s.count('G') * s.count('B')

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

Submission status: Accepted

Solution code:
def solve(n, s):
    D = {"R": 0, "G": 0, "B": 0}
    for c in s:
        D[c] += 1
    ans = D["R"] * D["G"] * D["B"]
    for i in range(n):
        for j in range(i+1,(i+n+1)//2):
            k = 2*j - i
            if (s[i] != s[j]) and (s[j] != s[k]) and (s[k] != s[i]):
                ans -= 1
    return ans

n = int(input())
s = input()
print(solve(n, s))

Submission status: Accepted

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
n=int(input())
S=input()
rt,gt,bt=S.count("R"),S.count("G"),S.count("B")
R,G,B=[],[],[]
rc,gc,bc=0,0,0
ans=0
for i in range(n):
  if S[i]=="R":
    ans+=gc*(bt-bc)+bc*(gt-gc)
    for b in B:
      if 2*i-b<n and S[2*i-b]=="G":
        ans-=1
    for g in G:
      if 2*i-g<n and S[2*i-g]=="B":
        ans-=1
    R.append(i)
    rc+=1
  elif S[i]=="G":
    ans+=rc*(bt-bc)+bc*(rt-rc)
    for b in B:
      if 2*i-b<n and S[2*i-b]=="R":
        ans-=1
    for r in R:
      if 2*i-r<n and S[2*i-r]=="B":
        ans-=1
    G.append(i)
    gc+=1
  else:
    ans+=gc*(rt-rc)+rc*(gt-gc)
    for r in R:
      if 2*i-r<n and S[2*i-r]=="G":
        ans-=1
    for g in G:
      if 2*i-g<n and S[2*i-g]=="R":
        ans-=1
    B.append(i)
    bc+=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(n):
    for d in range(n):
        if i+(2*d) >= n: break
        if s[i] != s[i+d] and s[i] != s[i+(2*d)] and s[i+d] != s[i+(2*d)]:
            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(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)