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: Time Limit Exceeded

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 = 0
for r in R:
    for g in G:
        for b in B:
            l = [r, g, b]
            l = sorted(l)
            if l[1] - l[0] != l[2] - l[1]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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


count= n*(n-1)*(n-2)/6

for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,n):
            if k-j==j-i:
                count-=1
            else:
                if s[i]==s[j] or s[j]==s[k] or s[i]==s[k]:
                    count-=1
print(int(count))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(0,N):
    for j in range(i,N):
        if S[i] != S[j]:
            for k in range(j,N):
                if  j-i != k-j and S[i] != S[k] and S[j] != S[k]:
                    ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

    ans = 0
    for k in range(N):
        for j in range(k):
            for i in range(j):
                if j-i != k-j:
                    if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                        ans+=1
    print(ans)
main()

Submission status: Time Limit Exceeded

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)
t=0
for i in range(len(r)):
    for j in range(len(g)):
        for k in range(len(b)):
            if abs(r[i]-g[j])!=abs(b[k]-g[j]) and abs(r[i]-b[k])!=abs(b[k]-g[j]) and abs(r[i]-g[j])!=abs(b[k]-r[i]):
                t=t+1
print(t)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
ans=0
for j in range(1,n-1):
  for i in range(j):
    for k in range(j+1,n):
      if j-i==k-j:
        continue
      if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    n=int(input())
    s=input()
    ans=0
    d=dict()
    dr=[0]*n
    dg=[0]*n
    db=[0]*n
    mr=[0]*n
    mg=[0]*n
    mb=[0]*n

    for i in range(n):
        if s[i]=='R':
            dr[i]=1
            mr[i]=1
        if s[i]=='G':
            dg[i]=1
            mg[i]=1
        if s[i]=='B':
            db[i]=1
            mb[i]=1

    for i in range(n-1):
        mr[n-2-i]+=mr[n-1-i]
        mg[n-2-i]+=mg[n-1-i]
        mb[n-2-i]+=mb[n-1-i]

    for i in range(n-2):
        for j in range(i,n-1):
            #print(ans,i,j,s[i],s[j])
            k=3
            if s[i]==s[j]:
                continue
            if s[i]=='R' and s[j]=='G':
                k=1
            elif s[i]=='R' and s[j]=='B':
                k=2
            elif s[i]=='G' and s[j]=='R':
                k=1
            elif s[i]=='G' and s[j]=='B':
                k=0
            elif s[i]=='B' and s[j]=='R':
                k=2
            elif s[i]=='B' and s[j]=='G':
                k=0
            #print(k)
            
            if k==0:
                ans+=mr[j+1]
                if (j+(j-i))<n:
                    ans-=dr[j+(j-i)]
            if k==1:
                ans+=mb[j+1]
                if (j+(j-i))<n:
                    ans-=db[j+(j-i)]
            if k==2:
                ans+=mg[j+1]
                if (j+(j-i))<n:
                    ans-=dg[j+(j-i)]

    print(ans)
        



main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
count=0
for k in range(3,N+1):
    for j in range(2,k):
        for i in range(1,j):
            if S[i-1]!=S[j-1] and S[j-1]!=S[k-1] and S[k-1]!=S[i-1] and j-i!=k-j:
                count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math

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

R=dict()
G=dict()
B=dict()

for i in range(N):
    if S[i]=='R':
        R[i]=i
    elif S[i]=='G':
        G[i]=i
    else:
        B[i]=i

sum=0
for p in R.keys():
    for q in G.keys():
        for r in B.keys():
            L=[int(p),int(q),int(r)]
            L.sort(key=int)
            if (L[1]-L[0] != L[2]-L[1]):
                sum+=1
print(sum)

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(N):
    for j in range(i,N):
        if S[i]==S[j]:
            continue
        else:
            for k in range(j,N):
                if j-i==k-j:
                    continue
                else:
                    if  S[i]==S[k] or S[k]==S[j]:
                        continue
                    else:
                        ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        for ib in range(ia+1, n-1):
            ta = s[ia]
            if s[ia] == s[ib]:
                pass
            else:
                sa = ib - ia
                cnt = 1
                ts = s[ib+1:]
                for ets in ts:
                    if cnt == sa:
                        pass
                    elif s[ib] != ets != ta:
                        r += 1
                    cnt += 1

    print(r)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

my_dict = {}
for i, c in enumerate(s):
    if not c in my_dict:
        my_dict[c] = []
    my_dict[c].append(i)

lst = []
for key, value in my_dict.items():
    lst.append(value)

if 3 > len(lst):
    print(0)
    exit(0)

cnt = 0
for i in lst[0]:
    for j in lst[1]:
        for k in lst[2]:
            i_, j_, k_ = sorted([i, j, k])
            if j_ - i_ != k_ - j_:
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

cnt = 0

for r in R:
    for g in G:
        for b in B:
            m = sorted([r,g,b])
            if m[1] - m[0] != m[2] - m[1]:
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

def linput():
    return [int(x) for x in input().split()]

n = int(input())
s = input().rstrip()

from collections import Counter

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)

cr = Counter(r)
cg = Counter(g)
cb = Counter(b)

import bisect
from copy import deepcopy
ans = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] == s[j]:
            continue
        ng = j + (j - i)
        if s[i] == "R" and s[j] == "G":
            l = deepcopy(b)
            c = cb
        elif s[i] == "R" and s[j] == "B":
            l = deepcopy(g)
            c = cg
        elif s[i] == "G" and s[j] == "R":
            l = deepcopy(b)
            c = cb
        elif s[i] == "G" and s[j] == "B":
            l = deepcopy(r)
            c = cr
        elif s[i] == "B" and s[j] == "R":
            l = deepcopy(g)
            c = cg
        elif s[i] == "B" and s[j] == "G":
            l = deepcopy(r)
            c = cr
        ans += len(l) - bisect.bisect_right(l, j)
        ans -= c[j + (j - i)]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
 
ans = 0
for i in range(0, N - 2):
    for j in range(i + 1, N - 1):
        if S[i] == S[j]:
            continue
        for k in range(j + 1, N):
            if j - i == k - j:
                continue
            if S[i] != S[k] and S[j] != S[k]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools as it

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

s = [int(i) for i in s.replace('R', '0').replace('G', '1').replace('B', '2')]
r_indeces = [i for i in range(n) if s[i] == 0]
g_indeces = [i for i in range(n) if s[i] == 1]
b_indeces = [i for i in range(n) if s[i] == 2]

indeces = [(i) for i in it.product(r_indeces, g_indeces, b_indeces)]
indeces = [i for i in indeces if (max(i) + min(i)) / 2 not in i]
print(len(indeces))

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

a = list(itertools.combinations(range(1,n+1),3))

ans = 0
for i in a:
    if (s[i[0]-1] != s[i[1]-1] and  s[i[2]-1] != s[i[1]-1] and  s[i[0]-1] != s[i[2]-1] and (i[2]-i[1] != i[1] - i[0])):
        #print(i)
        ans +=1

print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            if not S[i]==S[j] and not S[i]==S[k] and not S[j]==S[k] and not j-i == k-j:
                A += 1

print(A)

Submission status: Time Limit Exceeded

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):
  x=S[i]
  for j in range(i+1,N):
    if(S[j]!=x):
      y=S[j]
      if(2*j-i>=0 and 2*j-i<N):
        if(S[2*j-i]!=x and S[2*j-i]!=y):
          ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations, product

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

R_indices = [i for i in range(N) if S[i] == "R"]
G_indices = [i for i in range(N) if S[i] == "G"]
B_indices = [i for i in range(N) if S[i] == "B"]

count = 0
for indices_tuple in permutations([R_indices, G_indices, B_indices]):
    for i, j, k in product(*indices_tuple):
        if i < j < k and j - i != k - j:
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
# print(s[0], s[1], s[2])
i = 1
cnt = 0
while i <= n - 3:
    j = i + 1
    while j <= n - 1:
        k = j + 1
        while k <= n:
            if (
                j - i != k - j
                and s[i - 1] != s[j - 1]
                and s[j - 1] != s[k - 1]
                and s[i - 1] != s[k - 1]
            ):
                cnt = cnt + 1
            k = k + 1
        j = j + 1
    i = i + 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        if S[i] != S[j]:
            for k in range(j+1, N):
                if j-i != k-j and S[i] != S[k] and S[j] != S[k]:
                    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
import sys
import math
import string
import collections
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

N = int(input())
K = list(input())
a = collections.Counter(K)
ans = 0
R = list(itertools.accumulate([1 if K[i] == "R" else 0 for i in range(N)])) + [0]
G = list(itertools.accumulate([1 if i == "G" else 0 for i in K])) + [0]
B = list(itertools.accumulate([1 if i == "B" else 0 for i in K])) + [0]
for i in range(N):
    if K[i] == "R":
        ans += G[i - 1] * B[i - 1]
    elif K[i] == "G":
        ans += R[i - 1] * B[i - 1]
    else:
        ans += R[i - 1] * G[i - 1]
for k in range(2, N):
    for i in range(0, k):
        j = (k + i) / 2
        if j == (k + i) // 2 and j > i:
            j = int(j)
            if K[k] != K[i] and K[k] != K[j] and K[i] != K[j]:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

count = 0
for i in range(n):
    l1 = s[i]
    for j in range(i + 1, n):
        if s[j] == l1:
            continue
        else:
            l2 = s[j]
        for k in range(j + 1, n):
            if s[k] == l1 or s[k] == l2 or (j - i) == (k - j):
                continue
            else:
                count += 1
print(count)

Submission status: Time Limit Exceeded

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

ans = 0

for r in R:
    for g in G:
        for b in B:
            a,b,c = sorted([r,g,b])
            if b - a != c - b:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools


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

    ans = 0
    for v in itertools.permutations(range(N), 3):
        if v[0] < v[1] < v[2]:
            if (v[2] - v[1]) != (v[1] - v[0]):
                if (S[v[0]] != S[v[1]]) and (S[v[1]] != S[v[2]]) and (S[v[0]] != S[v[2]]):
                    ans += 1
    print(ans)
    return 0

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
count = 0
for i in range(N):
    for j in range(i+1, N):
        for k in range(j+1, N):
            if (S[i] != S[j]) and (S[k] != S[i]) and ( S[k] != S[j]) and ( j-i != k-j):
                count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
ans = 0
d={'R':0,'G':0,'B':0}
R,G,B=[],[],[]
for i in S:
    d[i]+=1
    R.append(d['R'])
    G.append(d['G'])
    B.append(d['B'])
for i in range(N-2):
    for j in range(i+1,N-1):
        s = [S[i],S[j]]
        if len(set(s))==2:
            if 'R' not in s:
                ans += R[-1]-R[j]
            elif 'G' not in s:
                ans += G[-1]-G[j]
            else:
                ans += B[-1]-B[j]
            if 0<=2*j-i<N and len(set([S[i],S[j],S[2*j-i]]))==3:
                ans -=1
print(ans)

Submission status: Time Limit Exceeded

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

    ans = 0

    for i in range(N):
        for ii in range(i, N):
            for iii in range(ii, N):
                if ((ii - i) == (iii - ii)):
                    continue
                if (len(set([S[i], S[ii], S[iii]])) == 3):
                    ans += 1

    print(ans)


resolve()

Submission status: Time Limit Exceeded

Solution code:

N = int(input())

S = input()
count = 0

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


print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys, math

n = int(sys.stdin.readline().strip())
s = sys.stdin.readline().strip()
res = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        if s[j] == s[i]:
            continue
        for k in range(j+1, n):
            if s[k] == s[i] or s[k] == s[j] or j-i == k-j:
                continue
            res += 1
print(res)

Submission status: Time Limit Exceeded

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

ans = 0
for j in range(1, n):
    m = s[j]
    for i in range(j):
        l = s[i]
        for k in range(j + 1, n):
            r = s[k]
            if 2 * j == i + k:
                continue
            else:
                if m != l and m != r and l != r:
                    ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
c = 0
for i in range(n):
    for j in range(i, n):
        if s[i] == s[j]:
            continue
        for k in range(j, n):
            if s[i] == s[k] or s[j] == s[k]:
                continue
            elif (j + 1) - (i + 1) != (k + 1) - (j + 1):
                c += 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
import sys
from io import StringIO
import unittest

def resolve():
    n = int(input())
    s = input()
    ans = 0

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

    print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """4
RRGB"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"""
        output = """1800"""
        self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(str(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)
ans=0
for r in R:
	for g in G:
		for b in B:
			p=r+g+b
			if p==3*r or p==3*g or p==3*b:
				pass
			else:
				ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(N-3):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            if j-i != k-j:
                if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
count=0
for i in range(N):
    for j in range(i,N):
        for k in range(j,N):
            if (j-i)!=(k-j) and S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


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

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

    print(result)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    n = int(input())
    s = input()
    rlist = []
    glist = []
    blist = []
    for i in range(n):
        if s[i] == "R":
            rlist.append(i)
        elif s[i] == "G":
            glist.append(i)
        else:
            blist.append(i)
    ans = 0
    for i in range(len(rlist)):
        r = rlist[i]
        for j in range(len(glist)):
            g = glist[j]
            for k in range(len(blist)):
                b = blist[k]
                count = sorted([r, g, b])
                if count[2]-count[1] != count[1]-count[0]:
                    ans += 1
    print(ans)
resolve()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
from sys import setrecursionlimit
from collections import Counter, deque, defaultdict
from bisect import bisect_left
setrecursionlimit(1000000)

# INF = float('inf')
INF = int(1e10)

def input():
    return stdin.readline()[:-1]

def counter(S, L1, L2, L3):
    ans = 0
    for ir in L1:
        j = bisect_left(L2, ir)
        for ig in L2[j:]:
            if S[ir] == S[ig]:
                continue
            k = bisect_left(L3, ig)
            ng = ig + (ig - ir)
            ans += len(L3[k:])
            if ng in L3[k:]:
                ans -= 1
    return ans


def main():
    from builtins import int, map
    N = int(input())
    S = input()
    iR, iG, iB = [], [], []
    for i in range(N):
        if S[i] == 'R':
            iR.append(i)
        elif S[i] == 'G':
            iG.append(i)
        elif S[i] == 'B':
            iB.append(i)

    # print(iR)
    # print(iG)
    # print(iB)

    ans1 = counter(S, iR, iG, iB)
    ans2 = counter(S, iR, iB, iG)
    ans3 = counter(S, iG, iR, iB)
    ans4 = counter(S, iG, iB, iR)
    ans5 = counter(S, iB, iR, iG)
    ans6 = counter(S, iB, iG, iR)
    # print(ans1)
    # print(ans2)
    # print(ans3)
    # print(ans4)
    # print(ans5)
    # print(ans6)
    ans = ans1 + ans2 + ans3 + ans4 + ans5 + ans6
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
if(N<=3):
  print(0)
else:
  for i in range(N-2):
    for j in range(i, N-1):
      for k in range(j, N):
        if(S[i]!=S[j] and S[j]!=S[k] and S[i]!=S[k] and j-i!=k-j):
          ans += 1
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
ans = 0
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)
import itertools
x = list(itertools.product(R,G,B))
for i in x:
    i = sorted(i)
    if i[1] - i[0] != i[2] - i[1]:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(len(s)):
	for j in range(i+1,len(s)):
		if s[i] != s[j]:
			for k in range(j+1,len(s)):
				if s[j] != s[k] and s[i] != s[k]:
					if j-i != k-j:
						total += 1

print(total)

Submission status: Time Limit Exceeded

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

a = 0

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

print(a)

Submission status: Time Limit Exceeded

Solution code:
N = int(input().strip())

S = input()

count = [[0,0,0] for i in range(N)]
for i in range(N-1, 0, -1):
    for j in range(3):
        count[i-1][j] = count[i][j]
    if S[i] == "R":
        count[i-1][0] += 1
    elif S[i] == "G":
        count[i-1][1] += 1
    elif S[i] == "B":
        count[i-1][2] += 1
"""
for j in range(3):
    for i in range(N):
        print(count[i][j], end = " ")
    print()
"""
ans = 0
for i in range(N):
    for j in range(i + 1, N):
        if S[i] == S[j]:
            continue
        if (S[i] == "R" and S[j] == "G") or (S[i] == "G" and S[j] == "R"):
            ans += count[j][2]
            diff = j-i
            if j+diff < N and S[j+diff] == "B":
                ans -= 1
        elif (S[i] == "B" and S[j] == "R") or (S[i] == "R" and S[j] == "B"):
            ans += count[j][1]
            diff = j-i
            if j+diff < N and S[j+diff] == "G":
                ans -= 1
        elif (S[i] == "G" and S[j] == "B") or (S[i] == "B" and S[j] == "G"):
            ans += count[j][0]
            diff = j-i
            if j+diff < N and S[j+diff] == "R":
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

s = list(input())

res = 0
p = 0
p2 = 0

for i in range(n):
    fst = s[i]
    #print(i)
    p = i+1
    while (p < n):
        #print(p)
        
        if(s[p] != s[i]):
            scd = s[p]
            p2 = p+1
            while (p2 < n):
                if(s[p2] != fst and s[p2] != scd):
                    if(p-i != p2 -p):
                        res += 1
                
                p2 += 1
        
        p += 1
        
        
print(res)

Submission status: Time Limit Exceeded

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

for i in range(len(s)):
    if s[i]=='R':
        r.append(i)
    elif s[i]=='G':
        g.append(i)
    elif s[i]=='B':
        b.append(i)
cnt=0
for _r in r:
    for _g in g:
        for _b in b:
            if _r<_g and _g<_b and (_g-_r!=_b-_g):
                cnt+=1
            elif _r<_b and _b<_g and (_g-_b!=_b-_r):
                cnt+=1
            elif _g<_b and _b<_r and (_r-_b!=_b-_g):
                cnt+=1
            elif _g<_r and _r<_b and (_b-_r!=_r-_g):
                cnt+=1
            elif _b<_r and _r<_g and (_g-_r!=_r-_b):
                cnt+=1
            elif _b<_g and _g<_r and (_r-_g!=_g-_b):
                cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
result = 0
for i in range(N):
    for j in range(i,N):
        for k in range(j,N):
            if j - i != k - j:
                if S[j] != S[i] and S[j] != S[k] and S[k] != S[i]:
                    result += 1

print(result)

Submission status: Time Limit Exceeded

Solution code:
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)

R.sort()
G.sort()
B.sort()

ans = len(R)*len(G)*len(B)
for r in R:
    for g in G:
        if r*2-g in B:
            ans -= 1
        if g*2-r in B:
            ans -= 1
        if (r+g)/2 in B:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(N):
    for j in range(i,N):
        if not S[i]==S[j]:
            for k in range(j,N):
                if not S[i]==S[k] and not S[j]==S[k] and not j-i==k-j:
                    ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# AtCoder Beginner Contest 162: D (2020/04/12)
N = int(input())
S = str(input())
cnt = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                if j-i != k-j:
                    cnt = cnt + 1
print(cnt)

Submission status: Time Limit Exceeded

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)
l=len(B)

ans=0
for i in range(len(R)):
    for j in range(len(G)):
        r=R[i]
        g=G[j]
        d=abs(r-g)
        cnt=0
        if d%2==0:
            if (r+g)//2 in B:
                cnt+=1
        if r<g:
            if g+d in B:
                cnt+=1
            if r-d in B:
                cnt+=1
        if r>g:
            if r+d in B:
                cnt+=1
            if g-d in B:
                cnt+=1
        ans+=l-cnt
print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

ans = 0
k = set('RGB')
for i in range(N):
	for j in range(i+1,N):
		ii = S[i]
		jj = S[j]
		if not ii==jj:
			tmp=list(k-set(ii+jj))[0]
			ans += S[j+1:].count(tmp)
			if 2*j-i<N and S[2*j-i]==tmp:
				ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
N=int(input())
S=input()
comb=0
if N<3:
	comb=0
else:
	for i in range(N-2):
		for j in range(i+1,N-1):
			for k in range(j+1,N):
				if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k] and (j-i)!=(k-j):
					comb+=1
print(comb)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
a,b,c,count=0,0,0,0

for i in range(N):
	a=S[i]
	for h in range(i+1,N):
		b=S[h]
		if b==a:
			continue
		for g in range(h+1,N):
			c=S[g]
			if c==b or c==a or h-i == g-h:
				continue
			else:
				count+=1

print(count)

Submission status: Time Limit Exceeded

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

s = 0
for a in range(N):
    o = S[a]
    for b in range(a+1, N):
        p = S[b]
        if o == p:
            continue
        for c in range(b+1, N):
            q = S[c]
            if o != q and p != q:
                if b-a != c-b:
                    s += 1
print(s)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

sum = 0
for i in range(N-2):
    Si = S[i]
    for j in range(i+1, N-1):
        if Si == S[j]:
            continue
        else:
            Sj = S[j]

        for k in range(j+1, N):
            if Si != S[k] and Sj != S[k]:
                if j - i != k - j:
                    sum += 1

print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math

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

R=[]
G=[]
B=[]

for i in range(N):
    if S[i] == "R":
        R.append(i+1)
    elif S[i] == "G":
        G.append(i+1)
    elif S[i] == "B":
        B.append(i+1)
    
ans = 0
for i in range(len(R)):
    for j in range(len(G)):
        for k in range(len(B)):
            ar=sorted([R[i],G[j],B[k]])
            if ar[1]-ar[0] != ar[2]-ar[1]:
                ans+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
N=int(input())
S=list(input())
Pattern=[x for x in combinations(range(N),3) if x[2]-x[1] != x[1]-x[0]]
res = 0
for i in Pattern:
    if S[i[0]] != S[i[1]] and S[i[1]] != S[i[2]] and S[i[2]] != S[i[0]]:
        res += 1
print(res)

Submission status: Time Limit Exceeded

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

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

print(res)

Submission status: Time Limit Exceeded

Solution code:
#D
N=int(input())
S=str(input())
count=0
for i in range(N):
    for j in range(i+1,N):
        for k in range(j+1,N):
            if j-i!=k-j and S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
                count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
count = 0
if N <= 3:
    print('0')
else:
    for i in range(N-2):
        for j in range(i+1,N-1):
            for k in range(j+1,N):
                if S[i] != S[j] and S[j] != S[k] and S[k] != S[i] and j-i != k-j:
                    count += 1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
RGB = [0] * 3
for i in range(N):
  if S[i] == "R":
    RGB[0] += 1
  elif S[i] == "G":
    RGB[1] += 1
  else:
    RGB[2] += 1
    
ans = RGB[0] * RGB[1] * RGB[2]

for i in range(1, 2000):
  for j in range(1, N + 1):
    if (j + 2 * i) <= N:
      if S[j - 1] != S[j + i - 1]:
        if S[j + 2 * i - 1] != S[j + i - 1]:
          if S[j - 1] != S[j + 2 * i - 1]:
            ans -= 1
            
print(max(ans, 0))