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

r = []
g = []
b = []
for i in range(len(s)):
    t = s[i]
    if t == 'R':
        r.append(i+1)
    elif t == 'G':
        g.append(i+1)
    else:
        b.append(i+1)

ans = len(r) * len(g) * len(b)

for i in range(1, int((n-1)/2)+1):
    for j in range(n-i-i, 0, -1):
        if s[j-1] != s[j+i-1] and s[j+i-1] != s[j+i+i-1] and s[j+i+i-1] != s[j-1]:
            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-2):
  for j in range(i+1, n-1):
    k = 2*j - i
    if k < n and 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()
cnt=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[i]!=S[j]!=S[k]!=S[i]:
            cnt-=1
print(cnt)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
s = input()

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

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

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
    if S[i] == 'G':
        g += 1
    if S[i] == 'B':
        b += 1

rgb_tot = r*g*b
#print(rgb_tot)

for i in range(N):
    first = S[i]
    for j in range(i,N):
        if 2*j - i < N:
            if first != S[j] and first != S[2*j-i] and S[j] != S[2*j-i]:
                #print("%d %d %d" % (i,j,2*j-i))
                #print(rgb_tot)
                rgb_tot -= 1

print(rgb_tot)

Submission status: Accepted

Solution code:
# triples of unique characters
# j - i != k - j

#from bisect import bisect_right

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

def solve(N, S):
    R = [1] if S[0] == "R" else [0]
    G = [1] if S[0] == "G" else [0]
    B = [1] if S[0] == "B" else [0]
    
    for i in range(1, N):
        char = S[i]

        if char == "R":
            R.append(R[-1] + 1)
            G.append(G[-1])
            B.append(B[-1])
        elif char == "G":
            R.append(R[-1])
            G.append(G[-1] + 1)
            B.append(B[-1])
        else:
            R.append(R[-1])
            G.append(G[-1])
            B.append(B[-1] + 1)
    
    count = 0

    for i in range(N-2):
        for j in range(i+1, N-1):
            if S[i] == S[j]:
                continue
            
            if S[i] != "R" and S[j] != "R":
                prefix = R
                char = "R"
            elif S[i] != "G" and S[j] != "G":
                prefix = G
                char = "G"
            else:
                prefix = B
                char = "B"

            k = 2*j-i

            count += prefix[-1] - prefix[j]
            if k < N and S[k] == char:
                count -= 1     
                
    return count

print(solve(N, S))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
p=s.count('R')*s.count('G')*s.count('B')

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

Submission status: Accepted

Solution code:
import collections

N = int(input())

S = list(input())
ScountList = collections.Counter(S)
result = ScountList['R'] * ScountList['G'] * ScountList['B']

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[i] != S[k]:
            result -= 1
            
print(result)

Submission status: Accepted

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

r = 0
g = 0
b = 0
for i in range(N):
  if S[i] == 'R':
    r += 1
  elif S[i] == 'G':
    g += 1
  else:
    b += 1

ans = r*g*b

for i in range(1,N-1):
  for j in range(1,min(i,N-i-1)+1):
    if S[i] != S[i-j] and S[i] != S[i+j] and S[i-j] != S[i+j]:
      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-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 = list(input())

r = sum([1 for x in s if x == 'R'])
g = sum([1 for x in s if x == 'G'])
b = sum([1 for x in s if x == 'B'])
ans = r * g * b

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

s_r = s.count("R")
s_g = s.count("G")
s_b = s.count("B")

def noteq(a, b, c):
  if a != b and b != c and c != a:
    return True
  else:
    return False

ans = s_r * s_g * s_b

for i in range(n):
  for d in range(n):
    j = i + d
    k = j + d
    if k >= n:
      break;
    else:
      a = s[i]
      b = s[j]
      c = s[k]
      if noteq(a, b, c):
        ans -= 1
    
print(ans)

Submission status: Accepted

Solution code:
import collections

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

aaa = collections.Counter(s)
s_r = aaa["R"]
s_g = aaa["G"]
s_b = aaa["B"]

def noteq(a, b, c):
  if a != b and b != c and c != a:
    return True
  else:
    return False

ans = s_r * s_g * s_b

for i in range(n):
  for d in range(n):
    j = i + d
    k = j + d
    if k >= n:
      break;
    else:
      a = s[i]
      b = s[j]
      c = s[k]
      if noteq(a, b, c):
        ans -= 1
    
print(ans)

Submission status: Accepted

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

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

ans= G*R*B

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

Submission status: Accepted

Solution code:
def contains(n, s):
	r, g, b = 0, 0, 0
	for i in s:
		if i == 'R':
			r += 1
		if i == 'G':
			g += 1
		if i == 'B':
			b += 1
	ans = r * g * b
	for i in range(len(s)):
		for j in range(i + 1, len(s)):
			k = j - i + j
			if k < len(s):
				if (s[i] == 'R' or s[j] == 'R' or s[k] == 'R') and (s[i] == 'G' or s[j] == 'G' or s[k] == 'G') and (s[i] == 'B' or s[j] == 'B' or s[k] == 'B'):
					# print(i, j, k)
					ans -= 1
	return ans


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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase

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 itertools import permutations
from bisect import bisect_left

input()
s = input()

d = defaultdict(list)

for idx, c in enumerate(s):
    d[c].append(idx)

ans = 0

for first, second, third in permutations("RGB"):
    for v1 in d[first]:
        for v2 in d[second]:

            if v2 < v1:
                continue

            ans += len(d[third]) - bisect_left(d[third], v2)
            dist = v2 - v1

            dist_idx_check = bisect_left(d[third], v2 + dist)

            if dist_idx_check < len(d[third]) and d[third][dist_idx_check] == v2 + dist:
                ans -= 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = [i for i in input()]
r = 0
g = 0
b = 0

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

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
bc = n*[0]
gc = n*[0]
rc = n*[0]
for i in range(1, n+1):
    mi = n - i
    if s[n - i] == 'B':
        if i == 1:
            bc[mi] = 1
        else:
            bc[mi] = bc[mi+1] + 1
            rc[mi] = rc[mi+1]
            gc[mi] = gc[mi+1]
    elif s[mi] == 'R':
        if i == 1:
            rc[mi] = 1
        else:
            rc[mi] = rc[mi+1] + 1
            bc[mi] = bc[mi+1]
            gc[mi] = gc[mi+1]
    elif s[mi] == 'G':
        if i ==1:
            gc[mi] = 1
        else:
            gc[mi] = gc[mi+1] + 1
            rc[mi] = rc[mi+1]
            bc[mi] = bc[mi+1]
si = 0
for i in range(0, n - 1):
    
    for j in range(i+1, n-1):
        if s[j] != s[i]:
            d = j-i 
            if j+d < n:
                nc = s[j+d]
                if nc in [s[i], s[j]]:
                    l = list(set(['R', 'G', 'B']) - set([s[i], s[j]]))
                    if l[0] == 'R':
                        si+= rc[j+1]
                    elif l[0] == 'G':
                        si += gc[j+1] 
                    else:
                        si += bc[j+1]
                else:
                    l = list(set(['R', 'G', 'B']) - set([s[i], s[j]]))
                    if l[0] == 'R':
                        si+= rc[j+1]-1
                    elif l[0] == 'G':
                        si += gc[j+1] -1
                    else:
                        si += bc[j+1] - 1
            else:
                l = list(set(['R', 'G', 'B']) - set([s[i], s[j]]))
                if l[0] == 'R':
                    si+= rc[j+1]
                elif l[0] == 'G':
                    si += gc[j+1] 
                else:
                    si += bc[j+1]
print(si)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import collections
from functools import reduce

def solve(N: int, S: str):
    c = collections.Counter(S)
    if len(c.keys()) < 3:
        print(0)
        return
    rgb = reduce(lambda x, y: x*y, [int(i) for i in c.values()])
    cnt = 0
    for i in range(N):
        for j in range(i + 1, N):
            k = 2 * j - i
            if k > N - 1:
                continue
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                cnt += 1
    print(rgb - cnt)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
r_pos = []
g_pos = []
b_pos = []

for i in range(n):
    st = s[i]
    if st == "R":
        r_pos.append(i)

    if st == "G":
        g_pos.append(i)

    if st == "B":
        b_pos.append(i)

colors = [r_pos, g_pos, b_pos]
if len(colors[0]) > len(colors[1]):
    colors[0], colors[1] = colors[1], colors[0]
if len(colors[0]) > len(colors[2]):
    colors[0], colors[2] = colors[2], colors[0]
if len(colors[1]) > len(colors[2]):
    colors[1], colors[2] = colors[2], colors[1]

x_num, y_num, z_num = len(colors[0]), len(colors[1]), len(colors[2])

ans = x_num * y_num * z_num
c = set(colors[2])
for a in colors[0]:
    for b in colors[1]:
        x, y = min(a, b), max(a, b)

        dif = y - x
        left_pos = x - dif
        right_pos = y + dif
        mid_pos = 4000
        if dif % 2 == 0:
            mid_pos = x + dif // 2

        left = 0
        right = 0
        mid = 0
        if left_pos in c:
            left = 1
        if right_pos in c:
            right = 1
        if mid_pos in c:
            mid = 1

        ans -= left + right + mid

print(ans)

Submission status: Accepted

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

R = [None] * S.count("R")
G = [None] * S.count("G")
B = [None] * S.count("B")

ri = 0
gi = 0
bi = 0
for i in range(N):
    if S[i] == "R":
        R[ri] = i+1
        ri+=1
    elif S[i] == "G":
        G[gi] = i+1
        gi += 1
    else:
        B[bi] = i+1
        bi += 1
R = set(R)
G = set(G)
B = set(B)

c = 0
for r in R:
    for g in G:
        for d in [(r + g) / 2, 2 * r - g, 2 * g - r]:
            if d in B:
                c += 1

ans = len(R) * len(G) * len(B) - c
print(ans)

Submission status: Accepted

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

# 各文字がどこまでにいくつ含まれるかを累積和で管理
R=[0]*N
G=[0]*N
B=[0]*N

dic={}
for c in ("R","G","B"):
  dic[c] = [0] * N

rp=0
gp=0
bp=0
for i in range(len(S)):
  if S[i]=="R":
    rp+=1
  elif S[i]=="G":
    gp+=1
  elif S[i]=="B":
    bp+=1
  dic["B"][i]=bp
  dic["R"][i]=rp
  dic["G"][i]=gp

def getRest(s1,s2):
  for c in ["R","G","B"]:
    if c not in (s1,s2):
      return c

ans = 0
for i in range(N-2):
  for j in range(i+1,N-1):
    # 最初の2つの文字を固定する
    if S[i] == S[j]:
      continue
    # 探すべき文字を特定
    rest = getRest(S[i],S[j])
    ans += dic[rest][-1] - dic[rest][j]
    
    if j + (j-i) < N and S[j + (j-i)] == rest:
      ans -= 1
    
print(ans)

Submission status: Accepted

Solution code:
# ABC162D - RGB Triplets

import numpy as np
from numba import njit

def main():
    n=int(input())
    s=np.zeros(n,dtype=np.int64)
    a=input()
    for i in range(n):
        s[i]=ord(a[i])%3
    ans=0
    d=np.zeros((3,n),dtype=np.int64)
    m=np.zeros((3,n),dtype=np.int64)
    l=np.array([[0,2,1],[2,1,0],[1,0,2]])
    #print(s.dtype,d.dtype,m.dtype)

    for i in range(n):
        d[s[i],i]=1
        m[s[i],i]=1

    for j in range(3):
        for i in range(n-1,0,-1):
            m[j,i-1]+=m[j,i]

    print(f(n,s,m,d,l))

@njit
def f(n,s,m,d,l):
    ans=0
    for i in range(n-2):
        for j in range(i+1,n-1):
            if s[i]==s[j]:
                continue
            ans+=m[l[s[i]][s[j]],j+1]
            if (j*2-i)<n:
                ans-=d[l[s[i]][s[j]],j*2-i]
    return ans
        
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

n = int(sys.stdin.readline().rstrip())
s = sys.stdin.readline().rstrip()

def main():
    cnt = [0] * 3
    for c in s:
      if c == 'R': cnt[0] += 1
      elif c == 'G': cnt[1] += 1
      else: cnt[2] += 1
    res = cnt[0] * cnt[1] * cnt[2]
    for i in range(n - 2):
      for j in range(i + 1, n - 1):
        k = 2 * j - i
        if k >= n: break
        res -= s[i] != s[j] and s[j] != s[k] and s[k] != s[i]
    print(res)

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
D = {"R": 1, "G": 2, "B": 4}
E = {"R": 0, "G": 1, "B": 2}
N = int(input())
T = [E[a] for a in input()]
S = [1 << t for t in T]
s = 0
for i in range(N):
    for j in range(1, min(i+1, N-i)):
        if S[i] + S[i-j] + S[i+j] == 7: s += 1

X = [[0] * N for _ in range(3)]

for i, t in enumerate(T):
    X[t][i] += 1

for i in range(3):
    for j in range(1, N):
        X[i][j] += X[i][j-1]

ans = -s
for i in range(N):
    if T[i] == 0:
        ans += X[1][i] * (X[2][-1] - X[2][i]) + X[2][i] * (X[1][-1] - X[1][i])
    if T[i] == 1:
        ans += X[2][i] * (X[0][-1] - X[0][i]) + X[0][i] * (X[2][-1] - X[2][i])
    if T[i] == 2:
        ans += X[0][i] * (X[1][-1] - X[1][i]) + X[1][i] * (X[0][-1] - X[0][i])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
if N <= 3:
    print('0')
else:
    minuscount = 0
    Rcount = 0
    Gcount = 0
    Bcount = 0 
    for i in range(N):
        if S[i] == 'R':
            Rcount += 1
        elif S[i] == 'G':
            Gcount += 1
        else:
            Bcount += 1
        for j in range(1,N):
            if i+2*j>=N:
                break
            if S[i] != S[i+j] and S[i+j] != S[i+2*j] and S[i+2*j] != S[i]:
                minuscount += 1

    print(Rcount*Gcount*Bcount-minuscount)

Submission status: Accepted

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

d ={'R':set(), 'G':set(),'B':set()}


for i,x in enumerate(s):
    d[x].add(i+1)
    
# print(d)

ans = 1
for k in d:
    ans*=len(d[k])
    
for i in d['R']:
    for j in d['G']:
        if (i+j)%2 == 0:
            v = (i+j)//2
            if v in d['B']:ans-=1
                
for i in d['R']:
    for j in d['B']:
        if (i+j)%2 == 0:
            v = (i+j)//2
            if v in d['G']:ans-=1
                
for i in d['B']:
    for j in d['G']:
        if (i+j)%2 == 0:
            v = (i+j)//2
            if v in d['R']:ans-=1
                
    

    
    
    
    
print(ans)

Submission status: Accepted

Solution code:
def calc():
  n=int(input())
  s=list(input())
  cnt1=0
  cnt2=0
  cnt3=0
  for i in range(n):
      if s[i]=='R':
          cnt1+=1
          s[i]=1
      elif s[i]=='G':
          cnt2+=1
          s[i]=2
      elif s[i]=='B':
          cnt3+=1
          s[i]=4
  ans=cnt1*cnt2*cnt3
  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]+s[k]==7:
            ans-=1
  print(ans)
calc()

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
numr = s.count("R")
numg = s.count("G")
numb = s.count("B")
ans = numr*numb*numg
for i in range(len(s)):
    if s[i] == "R":
        for j in range(len(s)):
            if s[j] == "G":
                if i<j:
                    if i+2*(j-i) < n and  s[i+2*(j-i)] == "B":
                        ans -= 1
                    if i-(j-i) >= 0 and  s[i-(j-i)] ==  "B":
                        ans -= 1
                if i>j:
                    if i-2*(i-j) >= 0 and  s[i-2*(i-j)] == "B":
                        ans -= 1
                    if i+(i-j) < n and s[i+(i-j)] ==  "B":
                        ans -= 1
                if (i+j)%2 == 0 and s[(i+j)//2] == "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')
ans=R*G*B
for d in range(N):
    if 2*d>=N:
        break
    for i in range(N):
        if i+2*d>=N:
            break
        if S[i]!=S[i+d] and S[i+d]!=S[i+2*d] and S[i]!=S[i+2*d]:
            ans-=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
result = 0
'''
for i in range(N):
    print(result)
    for j in range(i+1,N):
        if S[i] != S[j]:
            for k in range(j ,N):
                if k- j == j - i:
                    continue
                if S[j] != S[k] and S[i] != S[k]:
                    result +=1
print(result)
'''
r = S.count('R')
g = S.count('G')
b = S.count('B')
target = set('RGB')
result = r * g * b
for i in range(N):
    for j in range(1, min(i, (N - i - 1)) + 1):
        if target == set(S[i] + S[i-j] + S[j + i]):
            result -= 1
print(result)

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
S = list(input())

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

ans = R * G * B
count = 0

for i in range(N - 2):
    for j in range(i + 1, N):
        k = j * 2 - i
        if k >= N:
            break
        if {S[i], S[j], S[k]} == {"R", "G", "B"}:
            count += 1
ans -= count
print(ans)

Submission status: Accepted

Solution code:
import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(input())
    S = input().strip()

    c = collections.Counter(S)
    ans = c["R"] * c["G"] * c["B"]

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
S=input()
s=0
s+=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[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
      s-=1
print(s)

Submission status: Accepted

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

C = [0]*3
for i in range(N):
    c = "RGB".index(S[i])
    C[c]+=1

K = 0
for i in range(N-2):
    for j in range(i,N):
        k = 2*j-i
        if k>=N:break
        if len(set([S[i],S[j],S[k]]))==3:
            K+=1
print(C[0]*C[1]*C[2]-K)

Submission status: Accepted

Solution code:
K = int(input())
S = list(input())
total = 0
R,G,B = 0,0,0

for a in range(0,K):
  if S[a]=="R":
    R += 1
  elif S[a]=="G":
    G += 1
  else :
    B += 1
    
total = R*G*B

for i in range(0,K):
  for j in range(i,K):
    k = 2*j-i
    if k<K:
      if S[i] != S[k] and S[i] != S[j] and S[j] != S[k]:
        total -= 1
print(total)

Submission status: Accepted

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

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

all = r * g * b
sub = 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]:
            sub += 1
print(all - sub)

Submission status: Accepted

Solution code:
N=int(input())
S=str(input())
list_R=[]
list_G=[]
list_B=[]
count=0
for i in range(N):
    if S[i]=="R":
        list_R.append(i)
    if S[i]=="G":
        list_G.append(i)
    if S[i]=="B":
        list_B.append(i)
for i in range(N):
    for j in range(N):
        if i+2*j>N-1:
            break
        if S[i]!=S[i+j] and S[i+j]!=S[i+j*2] and S[i+2*j]!=S[i]:
            count=count+1

print(len(list_B)*len(list_G)*len(list_R)-count)

Submission status: Accepted

Solution code:
# ABC162D - RGB Triplets
def main():
    n=int(input())
    s=input()
    ans=0
    d=[[0]*n for _ in range(3)]
    m=[[0]*n for _ in range(3)]
    l=[[0,2,1],[2,1,0],[1,0,2]]

    for i in range(n):
        d[ord(s[i])%3][i]=1
        m[ord(s[i])%3][i]=1

    for j in range(3):
        for i in range(n-1,0,-1):
            m[j][i-1]+=m[j][i]

    for i in range(n-2):
        for j in range(i+1,n-1):
            if s[i]==s[j]:
                continue
            ans+=m[l[ord(s[i])%3][ord(s[j])%3]][j+1]
            if j*2-i<n:
                ans-=d[l[ord(s[i])%3][ord(s[j])%3]][j*2-i]

    print(ans)
        
main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
bad_num = [0] * n
pair = 0
b_num = []
ans = 0
for i in range(n - 1):
    if s[i] == "B":
        b_num.append(i)
        continue
    for j in range(i + 1, n):
        if s[j] == "B" or s[j] == s[i]: continue
        pair += 1
        if 2 * i - j >= 0:
            bad_num[2 * i - j] += 1
        if (j - i) % 2 == 0:
            bad_num[(i + j) // 2] += 1
        if 2 * j - i < n:
            bad_num[2 * j - i] += 1
if s[-1] == "B":
    b_num.append(n - 1)

for k in b_num:
    ans += pair - bad_num[k]
print(ans)

Submission status: Accepted

Solution code:
import math

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

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

for dis in range(1, math.ceil(len(s) / 2)):
    for i in range(dis, len(s) - dis):
        if s[i-dis] != s[i] and s[i-dis] != s[i+dis] and s[i] != s[i+dis]:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
e=enumerate
_,s=open(0)
r,g,b=map(s.count,'RGB')
print(r*g*b-sum(c!=d!=s[i+j]!=c for i,c in e(s)for j,d in e(s[i:-1:2])))

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:
import math
import sys
import collections

def main():
    N=int(input())
    Tr=list(input())
    R=[]
    G=[]
    B=[]
    for i in range(N):
       if Tr[i]=="R":
           R.append(i)
           continue
       if Tr[i]=="G":
           G.append(i)
           continue
       if Tr[i]=="B":
           B.append(i)
           continue
    cnt=len(R)*len(G)*len(B)
    for j in range(N):
        for i in range(j):
            if 2*j-i<=N-1:
                if Tr[2*j-i]!=Tr[i] and Tr[2*j-i]!=Tr[j] and Tr[j]!=Tr[i]:
                    cnt-=1
    print(cnt)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
nR = 0
nG = 0
nB = 0
cnt = 0
for i in range(N):
  if S[i] == 'R':
    nR += 1
  elif S[i] == 'G':
    nG += 1
  else:
    nB += 1
  for j in range(i+1, N):
    k = j - i + j
    if k < N:
      L = [S[i], S[j], S[k]]
      if 'R' in L and 'G' in L and 'B' in L:
        cnt += 1
    else:
      break
print(nR*nG*nB-cnt)

Submission status: Accepted

Solution code:
n = int(input())
S = list(input())
R, G, B = set(), set(), 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)
cnt = len(R)*len(G)*len(B)
for r in R:
    for g in G:
        if 2*g-r in B:
            cnt -= 1
        if 2*r-g in B:
            cnt -= 1
        if (r+g) % 2 == 0 and (r+g)//2 in B:
            cnt -= 1
print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
 
S=list(input())
m=0
 
a=S.count("R")
b=S.count("G")
c=S.count("B")
 
num=a*b*c
 
for i in range(N-2):
  for j in range(i+1,N-1):
    k=2*j-i #削る条件
    if k>N-1:
      break
    elif S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
      m+=1
      
print(num-m)

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
S=[0]*5000
S=list(input())

S_count=Counter(S)
r=S_count["R"]
g=S_count["G"]
b=S_count["B"]

ans=0

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

print(r*g*b-ans)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
S = list(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)
ans = r*g*b

for i in R:
    for j in G:
        M = max(i,j)
        m = min(i,j)
        d = M - m
        x = (M+m)/2
        y = M + d
        z = m - d
        if bisect.bisect_left(B,x) != bisect.bisect_right(B,x):
            ans -= 1
        if bisect.bisect_left(B,y) != bisect.bisect_right(B,y):
            ans -= 1
        if bisect.bisect_left(B,z) != bisect.bisect_right(B,z):
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
import itertools
import time
N = int(input())
S = (input())
# start = time.time()
r_index_lis, g_index_lis, b_index_lis = [], [], []
for i, s in enumerate(S):
    if s == 'R':
        r_index_lis.append(i)
    elif s == 'G':
        g_index_lis.append(i)
    else:
        b_index_lis.append(i)
count = len(r_index_lis)*len(g_index_lis)*len(b_index_lis)
# print(time.time() - start)
# exit()

# for r in r_index_lis:
#     for g in g_index_lis:
#         diff = r - g
#         # count += len(b_index_lis)
#         if(min(r, g) - diff >= 0):
#             if S[min(r, g) - diff] == 'B':
#                 count -= 1
#         if(max(r, g) + diff < N):
#             if S[max(r, g) + diff] == 'B':
#                 count -= 1
#         if (r+g) % 2 == 0:
#             if S[int((r+g)/2)] == 'B':
#                 count -= 1

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


print(count)

Submission status: Accepted

Solution code:
N=int(input())

S=list(input())
m=0

a=S.count("R")
b=S.count("G")
c=S.count("B")

num=a*b*c

for i in range(N-2):
  for j in range(i+1,N):
    k=2*j-i #削る条件
    if k>N-1:
      break
    elif S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
      m+=1
      
print(num-m)

Submission status: Accepted

Solution code:
import sys
#import copy
#import numpy as np
#import itertools
import collections
#from collections import deque
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
#from scipy.sparse import csr_matrix

sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read
count = collections.Counter

def main():
    N = int(readline())
    S = input()

    values = list(count(S).values())
    if len(values) < 3:
        print(0)
        return
    
    ans = values[0] * values[1] * values[2]

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


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